If we try to print any Object wid Sop than its toString() get called.class Object is the base or super class of all java classes and toString() is defined there. So toString() is available to all java classes, if you do not override thn compiler call Objects's version of toString(), and if you write one in your class that will be get called.
the core is System.out.println(myObject); is same as System.out.println(myObject.toString()); On Sat, Aug 22, 2009 at 2:44 PM, Eyas Kopty <[email protected]> wrote: > each object or class derives from the "Object" class and it has toString() > method, when you try to println or use your instance as it it was a string > (casting)- it firsts checks - if it implements the toString method and if it > does it gets its value(returned value of toString), try to remove the > toString method from your class and see that it prints other data I think it > would print the class name . > > On Fri, Aug 14, 2009 at 10:07 AM, Caleb Josue Ruiz Torres < > [email protected]> wrote: > >> THANKS A LOT!ALL OF YOU , ARE THE BEST! >> >> 2009/8/13 Caleb Josue Ruiz Torres <[email protected]> >> >> >>> 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 -~----------~----~----~----~------~----~------~--~---
