aspose file tools*
The moose likes Beginning Java and the fly likes Object of class having toString() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Object of class having toString() method" Watch "Object of class having toString() method" New topic
Author

Object of class having toString() method

Jyoti Vaskar
Ranch Hand

Joined: Jun 30, 2009
Posts: 142

If an object of a class having toString() method, is printed, It prints String value returned by method??
How this happen??

Is object value is string, returned by tostring() method in the class??

can anybody please explain?


thanks
Jyo
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

What wrong in that now??? You are overriding the toString method of the method in your class and then calling your method toString??? What was the problem here??


[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
Jyoti Vaskar
Ranch Hand

Joined: Jun 30, 2009
Posts: 142
Nitish Bangera wrote:What wrong in that now??? You are overriding the toString method of the method in your class and then calling your method toString??? What was the problem here??


Yes I override the method in my class.

But I am confused for this below,
when I try to print the value of the object of class A i.e. System.out.println(a);(A a=new A())
this prints string "This is A tostring" which is returned by toSting() method.

Now is it the object value??
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
The toString() function is a way of -well- turning your Object into a string somehow. This can either be an explanation or whatever you feel makes sense.

The default implementation by the Object class throws back the instances' reference code.


JDBCSupport - An easy to use, light-weight JDBC framework -
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
System.out.println(a) and System.out.println(a.toString()) are equivalent. They both call toString which returns a string, which println prints. All objects in Java have a toString method because it is part of Object.


"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
Deepak Borania
Ranch Hand

Joined: Jul 28, 2009
Posts: 45
By overriding the toString(), you define what should be returned when you want to use you object as a string. This string can be anything you want. It may just return value of fields as a string, or anything else..

Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10535
    
    9

From the PrintStream source code:



and from the String source code


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Object of class having toString() method