File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Should toString() return a null ?

 
Jay Damon
Ranch Hand
Posts: 282
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have encountered some classes recently that can return a null value depending upon the object's state. I am wondering if this is an acceptable practice? I would say no, that toString() should always return a String and if null is the value you want to return, return "null". However, I was wondering what others more learned than I would have to say on this topic.
 
Serge Plourde
Ranch Hand
Posts: 140
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It all depends!

You should override toString() in order to display the "content" of an object.

If you use it along other displays, you might want just to return "" instead of null.
 
Gabriel White
Ranch Hand
Posts: 233
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java is smart enough that whenever you don't create a toString() method that when you instantiate an object in your main method and you want it to return something, you don't have to type in SOP(test.toString) you just have to type SOP(test) (test being the new object to the class). In order for it to return null you would have to do what Serge said and actually create the toString method and tell it to return null, but I wouldn't advise this because there would be no reason that I could see for toString to return null.

HTH

Gabe
 
Jay Damon
Ranch Hand
Posts: 282
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Serge/Gabriel,

I think you misunderstand the question. I don't want to create a class that returns a null value from toString(). I have inherited an application in which some of the classes do this. Personally, I think it is bad practice to return null from toString().

The Object.toString() Java says toString() "Returns a string representation of the object." IMO, null is not a string representation. "null" is a string representation.

I discovered this problem accidentally while creating a class to parse and display an object's String representation by invoking toString(). Because some objects return null, my class has to guard against this possibility by checking for a null value first. While that may be good practice, I think one should be able to expect toString() to actually return a String value.

I just wanted to seek other opinions.

Jay
 
Tim West
Ranch Hand
Posts: 539
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jay,

I'd agree with you entirely. IMO there is no circumstance under which the null-String gives any useful information about any class. If you're having to check for null values that's even worse! I'd say this is a "code smell" that should definitely be avoided.

If toString() is used for debugging (as IMHO it almost almost always should be), I reckon it should give at the minimum the name of the class, and whatever attribute is most defining for that instance.

Cheers,


--Tim
 
Tim West
Ranch Hand
Posts: 539
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An addition, if you have access to it you might like to check out Item 9 in Bloch's 'Effective Java', which deals specifically with toString(). Some of his notes:

  • Providing a good toString implementation makes your class much more pleasant to use.
  • When practical, the toString method should return all of the interesting information contained in the object.
  • Whether or not you decide to specify the format of what's returned by toString, you should clearly document your intentions.
  • Whether or not you specify the format, you should provide programmatic access to all of the information contained in the value returned by toString. This removes the temptation of programmers who use your class to parse the result of toString to gain access to this information.


  • The explanations for these points are all in the book, I'll leave them there as something to think about


    --Tim

    (Mods: I hope that isn't too close to the book to be considered plaigarism. Remove it if it is. I'd heartily recommend the book, and I intended that this post do more to recommend it than to prevent its purchase by including its content )
    [ June 23, 2004: Message edited by: Tim West ]
     
    Jim Yingst
    Wanderer
    Sheriff
    Posts: 18671
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The Object.toString() Java says toString() "Returns a string representation of the object." IMO, null is not a string representation.

    I agree, returning null is a violation of the spec. Plus it's just not relly useful to anyone. I can't think of any good reason to do this.
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic