On Aug 14, 1: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!
I'll give you a way to understand it: dissection of the
System.out.println(Object e); line
See the definition of out method in class System, then following the
call, see the definition of println(Object) method in class
PrintStream, then the definition of valueOf(Object) method in class
String.
Then go to class Object, and see the definition of toString method.
With that in hand, you should understand what happens and why it
happens so.
See the definition of method toString in class Object, then see the
definition of println(Object) in class PrintStream, then the
definition of println(Object) in class String, then the definition of
print(Object) in class String, then the definition of
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to