aspose file tools*
The moose likes Java in General and the fly likes Strings.... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Strings...." Watch "Strings...." New topic
Author

Strings....

Sophie Angela
Ranch Hand

Joined: Jul 17, 2006
Posts: 46
What is the purpose of a toString() method..? Im getting confused with it...In what ways that method is used..?Is there any special criteria a class has to satisfy to override a toString() method? What are all the classes that have overriden the toString() method..?
bhaswar goswami
Greenhorn

Joined: Jul 04, 2005
Posts: 14
Actuallt this toString() should be overridden in almost all the user defined classes.
The purpose of this method is to give you the String representation of the Java Objects on the runtime.

So, whenever you are wrtiting your classes then you should provide the
toString() implementation for the same also .

I hope that clarifies your doubt.


Regards,

Bhaswar Goswami
SCJP 5.0 , SCDJWS


Richard Green
Ranch Hand

Joined: Aug 25, 2005
Posts: 536


will give you Office@Der3afc or something like that

Now if you override the toString() method,



will result in Office [10,ABC Corporation]

toString() is very helpful for debugging purposes. "Office [10,ABC Corporation]" makes more sense than "Office@Der3afc"


MCSD, SCJP, SCWCD, SCBCD, SCJD (in progress - URLybird 1.2.1)
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Originally posted by Jaiganesh Elumalai:
What is the purpose of a toString() method..? Im getting confused with it...In what ways that method is used..?Is there any special criteria a class has to satisfy to override a toString() method? What are all the classes that have overriden the toString() method..?


Depends who you ask. If you ask me, I'll tell you that it is an unfortunate defect in the language and API that can best be overcome by declaring it to fail immediately at runtime should it ever be invoked - not ideal but a best case scenario nevertheless. This is what I do for toString (and other defects that are implicitly inherited such as equals/hashCode) and it works relatively well. Others will almost certainly disagree arguing with a premise that I claim is itself flawed.


Tony Morris
Java Q&A (FAQ, Trivia)
Adam Nace
Ranch Hand

Joined: Jul 17, 2006
Posts: 117
Originally posted by bhaswar goswami:
Actuallt this toString() should be overridden in almost all the user defined classes.
The purpose of this method is to give you the String representation of the Java Objects on the runtime.

So, whenever you are wrtiting your classes then you should provide the
toString() implementation for the same also .

I hope that clarifies your doubt.


I have to dissagree with that. There are many cases in which it makes no sense to override toString(). The default implementation of toString gives you the name of the class (and the hashCode, if I'm not mistaken). The only need to disambiguate that is if the class you are writing maintains a state. More specifically, if it maintains a public state. Otherwise, overriding toString() makes no sense. It already tells you what type of object it is, and thats all you would be telling the user if you override toString on an object without a public state.

You will notice that I have refered to "public state." This is important. Even if the object maintains state information, if that information is not part of the public interface of the class, DON'T provide it in your toString() method. Otherwise, clients will be tempted to parse toString() to get that information, which prevents you from later being able to change the implementation of toString(), or from being able to override it in a subclass.

For some examples of when NOT to override toString, consider this: Some classes maintain NO state. Does it make any sense to override toString in a Strategy Pattern? The only thing you could tell the client is that it is an instance of your concrete strategy, and the default implementation already does this. The same could be said about the visitor pattern. Another common use of toString() is to be able to differentiate between multiple instances of an object (because you don't want to actually parse the string to obtain state data). If your object is a Singleton, it doesn't always make sense to override toString, because there is no other instance to differentiate it from. In these situations, overriding toString() could be considered pollution to your code. It's extra noise that effectively accomplishes nothing.

And I don't think I need to explain why you shouldn't bother overriding toString() in classes that contain only static methods and fields (i.e. classes similar to java.Math).

Hope this helps!

- Adam
Vj Kamath
Greenhorn

Joined: Nov 19, 2004
Posts: 15
It can be used to display the "state" of any object just before an exception occured. Just display the name and instance variable values in toString and use it in the catch block for an exception related to that object.
Chengwei Lee
Ranch Hand

Joined: Apr 02, 2004
Posts: 884
Originally posted by Tony Morris:


Depends who you ask. If you ask me, I'll tell you that it is an unfortunate defect in the language and API that can best be overcome by declaring it to fail immediately at runtime should it ever be invoked - not ideal but a best case scenario nevertheless. This is what I do for toString (and other defects that are implicitly inherited such as equals/hashCode) and it works relatively well. Others will almost certainly disagree arguing with a premise that I claim is itself flawed.


Would you care to explain your reasons for saying so? Why do you think of them as a defect?


SCJP 1.4 * SCWCD 1.4 * SCBCD 1.3 * SCJA 1.0 * TOGAF 8
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Strings....