it's println that is calling the toString() method. When you have a toString() method in your class, println uses that method, when you don't, println uses the toString() that comes with object.
According to the spec (http://java.sun.com/j2se/1.4.2/docs/api/java/ lang/Object.html#toString()): The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. hence, you get 'MyPoint', '@', and a number in hex (19821f or addbf1) hth mcalex On Aug 14, 7:04 am, Caleb Josue Ruiz Torres <[email protected]> wrote: > this doubt don't let me sleep, help me to understand > > i got this Class > -------------------------------------------------------- > public class MyPoint { > public int x; > public int y; > > public String toString() { > return ("[" + x + "," + y + "]"); > }} > > --------------------------------------------------------- > > then i use this other Class to test the first one > --------------------------------------------------------- > public class TestMyPoint { > public static void main(String[] args){ > MyPoint start = new MyPoint(); > MyPoint end = new MyPoint(); > > start.x=10; > start.y=10; > end.x=20; > end.y=30; > > System.out.println("Start point is: " + start); > System.out.println("End point is: " + end); > } > > } > > ------------------------------------------------------------- > > and the ouput is: > Start point is: [10,10] > End point is: [20,30] > > WHY? if i didn't call the toString Method in any line > > then i change the name of the method toString to the name angelo (lol) > as follow > public class MyPoint { > public int x; > public int y; > > public String angelo() { > return ("[" + x + "," + y + "]"); > } > > } > > and the ouput was : > > Start point is: mypo...@19821f > End point is: mypo...@addbf1 > > please help me to understand this topic! --~--~---------~--~----~------------~-------~--~----~ To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/javaprogrammingwithpassion?hl=en -~----------~----~----~----~------~----~------~--~---
