Advertisement
Guest User

Untitled

a guest
May 6th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. public class Person {
  2. private String name;
  3.  
  4. // constructor and getter/setter omitted
  5. }
  6.  
  7. System.out.println(myPerson);
  8.  
  9. Person[] people = //...
  10. System.out.println(people);
  11.  
  12. System.out.println(myObject); // invokes myObject.toString()
  13.  
  14. // Code of Object.toString()
  15. public String toString() {
  16. return getClass().getName() + "@" + Integer.toHexString(hashCode());
  17. }
  18.  
  19. public class Person {
  20.  
  21. private String name;
  22.  
  23. // constructors and other methods omitted
  24.  
  25. @Override
  26. public String toString() {
  27. return name;
  28. }
  29. }
  30.  
  31. @Override
  32. public String toString() {
  33. return getClass().getSimpleName() + "[name=" + name + "]";
  34. }
  35.  
  36. Person[] people = { new Person("Fred"), new Person("Mike") };
  37. System.out.println(Arrays.toString(people));
  38.  
  39. // Prints: [Fred, Mike]
  40.  
  41. List<Person> people = new ArrayList<>();
  42. people.add(new Person("Alice"));
  43. people.add(new Person("Bob"));
  44. System.out.println(people);
  45.  
  46. // Prints [Alice, Bob]
  47.  
  48. {
  49. SomeClass sc = new SomeClass();
  50. // Class @ followed by hashcode of object in Hexadecimal
  51. System.out.println(sc);
  52. }
  53.  
  54. class A {
  55. String s = "I am just a object";
  56. @Override
  57. public String toString()
  58. {
  59. return s;
  60. }
  61. }
  62.  
  63. class B {
  64. public static void main(String args[])
  65. {
  66. A obj = new A();
  67. System.out.println(obj);
  68. }
  69. }
  70.  
  71. ReflectionToStringBuilder.toString(object)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement