9

This might be a very basic question, apologies if this was already asked. Should toString() in Java be used for actual program logic or is it only for debugging/human reading only. My basic question is should be using toString() or write a different method called asString() when I need to use the string representation in the actual program flow.

The reason I ask is I have a bunch of classes in a web service that rely on a toString() to work correctly, in my opinion something like asString() would have been safer.

Thanks

jack_carver
  • 1,510
  • 2
  • 13
  • 28
  • it is more for logging than debugging, its use. – RamonBoza Nov 11 '13 at 16:34
  • It is what it is, a string representation of an object. If your application lives only on the command line, then it can certainly be part of actual code. Like having to print a tic-tac-toe board after every move. I've also made toString methods for entering data into databases too. Those rows of which are used for logic at some other time in the future. So I think the answer to this question should be yes you can use it for logic. – Cruncher Nov 11 '13 at 16:35
  • this post might be helpful: http://stackoverflow.com/questions/3615721/how-to-use-the-tostring-method-in-java – tokhi Nov 11 '13 at 16:38
  • 2
    It depends on the class. Consider StringBuilder#toString, which is certainly not for debugging. – Ingo Nov 11 '13 at 16:38

2 Answers2

13

Except for a few specific cases, the toString should be used for debugging, not for the production flow of data.

The method has several limitations which make it less suitable for use in production data flow:

  • Taking no parameters, the method does not let you easily alter the string representation in response to the environment. In particular, it is difficult to format the string in a way that is sensitive to the current locale.
  • Being part of the java.Object class, this method is commonly overridden by subclasses. This may be harmful in situations when you depend on the particular representation, because the writers of the subclass may have no idea of your restrictions.

The obvious exceptions to this rule are toString methods of the StringBuilder and the StringBuffer classes, because these two methods simply make an immutable string from the mutable content of the corresponding object.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • There are a lot more exceptions, Swing is (in)famously making use of toString() in practically every default model/renderer it provides. Its a very mixed bag. Despite its limitations the use of toString() for purposes other than debugging is relatively widespread (and in some cases unavoidable due to how API were defined). – Durandal Nov 11 '13 at 17:36
  • 2
    @Durandal The (ab-)use of toString in other libraries is not sufficient reason to repeat that mistake. – tkruse Dec 03 '19 at 00:57
0

It is not just for debugging/human reading only, it really depends on the context in which the object is being used. For example, if you have a table which is displaying some object X, then you may want the table to display a readable textual representation of X in which case you would usually implement the toString() method. This of course is a basic example but there are many uses in which case implementing toString() would be a good idea.

patois50
  • 44
  • 1
  • Thank for your reply, but my question is where toString should be used in production for usecases other than logging/debugging extra, i.e should any business logic depend on it. – jack_carver Nov 11 '13 at 17:14