• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why does this java code do this?  RSS feed

 
Joe McTigue
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I'm trying to understand why the following code calls the local toString() method and not the testMethod()?


This prints out the string "43" as a result.

Even when I change the name of the method in the super class to toFString() it prints out the same returned string?
Why does the local toString() method even get called, I just ruled out it being because the super method had the same name.
...and why isn't it calling the testMethod() as well...???
Any help on understanding this would be greatly appreciated.

Regards,
Java Joe
 
Winston Gutkowski
Bartender
Posts: 10574
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe McTigue wrote:I'm trying to understand why the following code calls the local toString() method and not the testMethod()?

And I'm trying to understand why you think it would call testMethod(), since you never call it yourself.

There's a very good reason why it would call toString() though, which you can find in the docs for String.valueOf(Object) (which is called by println(Object)).

HIH

Winston
 
Joe McTigue
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Winston,

So the toString() local method is called by default from println() when println() is asked to print the object.
Thanks for explaining this and adding the reference. :-)

Thanks,
Java Joe
 
ikneet singh
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Joe

The toString() method is provided by the Object class so that each object can be represented meaningfully if you do not override it then it may spit out smething like, "SubClass@100ab23". In your program you are using System.out.println(new SubClass()) which will invoke the toStirng() method of the SubClass. Inside toStirng() using super.toString() which in will further call the toString() method of the Super class which returen 4 and inside SubClass's toString() method a 3 is appended to it and 43 is printed.

The main point is whenever you try to use an object or reference variable of an object inside the println() method it will make an implicit call to object's toString() method of that object.

 
Joe McTigue
Ranch Hand
Posts: 73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Ikneet Singh,

I really appreciated your description on why and how this works, it really helped me understand the process better.

Regards,
Joe
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ikneet singh wrote:The main point is whenever you try to use an object or reference variable of an object inside the println() method it will make an implicit call to object's toString() method of that object.


It's not implicit. System.out is a PrintStream. This is from PrintStream.java: and this is the method it calls in the String class:
 
ikneet singh
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Darryl,

Thanks for the correction . Actually, I was referring to the same API which you mentioned, but I guess I was unable to make it clear thanks for doing so. The toString() method is called by the valueOf() method of the String class if the object passed is not null.
Cheers.

Regards,
Ikneet Singh
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!