2

Since myObject.toString() fails when myObject is null (throws a NullPointerException), it is safer to do myObject+"", since it's essentially doing String.valueOf(myObject).concat(""), which doesn't fail, but would instead result in the String "null".

However, is this a good practice? My first thought is that it seems like it might take longer to perform, since it's implicitly calling two methods, but it does help guarantee software that doesn't crash.

Ky -
  • 30,724
  • 51
  • 192
  • 308
  • Are you using Java 7 or 6? – Rohit Jain Feb 06 '14 at 18:59
  • 3
    The good practice is to check whether or not myObject is null before calling toString – Amir Keibi Feb 06 '14 at 19:01
  • 2
    Years ago I remember learning about the `+""` and hearing (I think from a talk with Josh Bloch that he felt guilty about using it until he saw that the `java.net.*` Java Libraries use it in a few places – Jason Sperske Feb 06 '14 at 19:03
  • @Amir that should be an answer not a comment! – Martin Dinov Feb 06 '14 at 19:03
  • @MartinDinov No that is a comment. Null check is not the modern praxis. avoiding nulls is better – AlexWien Feb 06 '14 at 19:04
  • @AlexWien isn't that a bit too firm. I mean if I want to be pragmatic I'd consider what works best in each scenario. Avoiding nulls isn't always better. – Amir Keibi Feb 06 '14 at 19:07
  • 1
    After trying to track down some more authoritative source than my fragile memory, I think the advice appears to have pertained to converting `int`s to `String`s and also appears to be wrong (who knows if Josh Bloch was ever wrong, the guy literally wrote the book on Effective Java), but I think @RohitJain is on the right track with his answer. – Jason Sperske Feb 06 '14 at 19:08
  • It is modestly inefficient, but probably not a poor choice for the oddball cases where you need this sort of thing. – Hot Licks Feb 06 '14 at 19:09

2 Answers2

8

You certainly can do myObject+"". But as you already know, that requires some extra method invocation. That will depend upon the application you're using it in. Will that extra method invocation be a bottle-neck for the application? I guess that is rarely an issue. Or, to avoid that extra method call, you can directly use String#valueOf() method. But that would depend upon how you want to handle nulls. I would certainly not proceed normally in these circumstances. At least log a message indicating null reference.

Also, if you're already on Java 7, then you can use Objects.toString(Object) method, that handles null for you. Again, that method returns "null" for null references.

So, now it's your call. I've given you some option. You might want to throw exception, log message and proceed with "null" string, or some default string like "".

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • returning a "null" string would avoid the immediate threat of failing. But what if the content ("null") cause more problem or take the code through a more disastrous route? – Amir Keibi Feb 06 '14 at 19:10
  • 1
    @AmirKeibi Yes, that will depend upon OP how he wants to handle `null`. We can just provide him/her options. – Rohit Jain Feb 06 '14 at 19:12
3

If you want that behavior, String.valueOf(myObject) gets you the same while being less hacky. But it also means that a null string and the string "null" are treated the same. It's usually better to check for null values explicitly, unless you're just print to a log or such. But in those cases, most methods take an Object reference and handle nulls for you (e.g. System.out.println, most logging frameworks, etc.)

yshavit
  • 42,327
  • 7
  • 87
  • 124