The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
October 2001



This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.



Posted by Matt Gerrans on October 31, 2001 at 12:38 PM

> I think Matt has summed it up very nicely. One other thing that I have noticed once or twice in my Java development career is code along the lines of:-

> String myString = "" + someArbitaryObject.getArbitaryValue();

> If the method getArbitaryValue() returns a null, you will get a string object created with the value "null". This has caused me hours of fun trying to debug code like this in the past, so it's worth watching out for.

Maybe you could avoid that problem by doing it this way:

String myString = someArbitaryObject.getArbitaryValue().toString();

This wouldn't work if the method returned a primative, but I assume it doesn't because in that case, it couldn't return null.

Except for primatives, where the various purmutations of (new Integer(i)).toString() is a lot more tedious than ("" + i), this trick is not nearly as necessary in Java as in JavaScript, where there is no other option (or maybe there is, but I never found it).

- mfg


Sponsored Links

Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us