This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes toString() method explaination Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "toString() method explaination" Watch "toString() method explaination" New topic
Author

toString() method explaination

vijay umar
Ranch Hand

Joined: Mar 24, 2009
Posts: 100
hey everyone

i have a simple doubt about the tostring() method,just consider this example

public class BobTest
{
public static void main (String[] args)
{
Bob f = new Bob("GoBobGo", 19);
System.out.println(f);
}
}
class Bob
{
int shoeSize;
String nickName;
Bob(String nickName, int shoeSize)
{
this.shoeSize = shoeSize;
this.nickName = nickName;

}

public String toString()
{
return ("I am a Bob, but you can call me " + nickName +". My shoe size is " + shoeSize);
}
}

the output of this program is "I am a Bob, but you can call me GoBobGo. My shoe size is 19"

i dint understand that how this program invoked the toString() method, it has no reference in the main method also? is it invoked automatically? how does it works? please explain?

avi sinha
Ranch Hand

Joined: Mar 15, 2009
Posts: 452



SCJP 5.0 SCWCD 5.0
avi sinha
Ranch Hand

Joined: Mar 15, 2009
Posts: 452

toString is a method of class "object"
every class we create implicitly extends the object class hence every class have a toString method
whenever you write System.out.println(xyz) it invokes the toString method
here you are overriding toString hence due to polymorphism the toString method you declared in the program is invoked and gives the output

avi
vijay umar
Ranch Hand

Joined: Mar 24, 2009
Posts: 100
thanks!thank you very much for replying!

So you are trying to tell that all the method which are present in the Object class are invoked automatically ?

and here in my example System.out.println(f); is responsible for the invocation of the toString() method? am i right?how does polymorphism comes into play here?
avi sinha
Ranch Hand

Joined: Mar 15, 2009
Posts: 452

vijay umar wrote:how does polymorphism comes into play here?

here if you dont override the toString method the System.out.println will call the original toString method inherited from the object class
but since you have overridden it that's why the overridden toString method will be called.
Prasad Kumbhare
Greenhorn

Joined: Oct 18, 2007
Posts: 26
Vijay,

Answer to your question, " here in my example System.out.println(f); is responsible for the invocation of the toString() method? am i right?how does polymorphism comes into play here? "

Javadoc of Print says,

print
public void print(Object obj)Print an object. The string produced by the String.valueOf(Object) method is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.

Now String.valueOf

valueOf
public static String valueOf(Object obj)Returns the string representation of the Object argument.

Parameters:
obj - an Object.
Returns:
if the argument is null, then a string equal to "null"; otherwise, the value of obj.toString() is returned.

That's why when you do System.out.print(f) where f is your obejct, toString method gets invoked.

I hope this helps.
Thanks


Thanks,
Prasad Kumbhare
SCJP 5.0, SCWCD 5.0, SCBCD 5.0
vijay umar
Ranch Hand

Joined: Mar 24, 2009
Posts: 100
hi avi and Prasad,

i have small clarification to be made on avi's post!

you said that if we dont override toString() method then it will be called automatically!

i did like that and i got the object in some form of@@$$gobobgo, it is byte form i suppose, but what i read in books is that toString() is used to convert the object into String format! is'nt it! so when it is called automatically when i havent overriden it then it should give me the string value as gobobgo and 19 as output isnt it!!
avi sinha
Ranch Hand

Joined: Mar 15, 2009
Posts: 452

vijay umar wrote:
you said that if we dont override toString() method then it will be called automatically!


i didn't understand what you want to say....

vijay umar wrote:but what i read in books is that toString() is used to convert the object into String format! is'nt it!


its correct !!!

avi@10b62c9

Process completed.



--------------------Configuration: <Default>--------------------
meaningful thing!!

Process completed.
vijay umar
Ranch Hand

Joined: Mar 24, 2009
Posts: 100
Thank you very much avi, now its clear
 
Don't get me started about those stupid light bulbs.
 
subject: toString() method explaination
 
Similar Threads
Internal Method Call ( toString)
how toString() works in this program???
Bad Form to name indentifier and method the same?
Access overriden Method
Printing Method (inheritance and super method)