21

I was checking String.valueOf method and found that when null is passed to valueOf it returns "null" string instead of pure java null.

My question is why someone will return "null" string why not pure java null.

public static String valueOf(Object obj) {
    return (obj == null) ? "null" : obj.toString();
}

I am asking this because developer will have to check with equals method for "null" string and not like stringObj != null.

[update]: okay there were down votes for this question, I am not raising question in the api but in my current company there are lot of api which returns "null" string instead of pure java null that is why I asked this question to know whether is it a best practice to return "null" string if we found null string object.

7
  • 2
    That's just how it was spec'd to behave. Useful mostly for printing (and it is very useful that System.out.println doesn't error out if passed null) Commented Nov 24, 2015 at 11:07
  • 1
    Don't pass a null in the first place? Commented Nov 24, 2015 at 11:08
  • "I am asking this because developer will have to check with equals method for "null" string and not like stringObj != null." If you're comparing Strings directly, just check the references. Otherwise it means that your equals method is based on the String representation of an object which is not good. Commented Nov 24, 2015 at 11:10
  • 1
    You can't print null but you can print "null" Commented Nov 24, 2015 at 11:12
  • 1
    You want a String from an object and you're confused when you get it? Commented Nov 24, 2015 at 11:19

4 Answers 4

30

You are looking for Objects#toString(java.lang.Object,java.lang.String)

The call

Objects.toString(obj, null)

returns null when object is null.

Sign up to request clarification or add additional context in comments.

Comments

23

Because String.valueOf() returns a String representation, which for a null is "null".

The developer shouldn't be checking the return value at all, since it's meant for display purposes, not for checking whether a reference was null or not.

1 Comment

bad design. String.valueOf("null") == String.valueOf(null).
1

Because you only want a string representation of null and so it did. Purpose of this method is to return the String representation.

Check link String.valueOf

Comments

-2

I would only use valueOf for displaying something directly. It doesn't offer robust enough validation and sanity checking to pass the output directly into a database, and could lead to incomplete data or data corruption.

Using the following query I discovered there were null-values in the columns.

Select * from tableName x where x.name is null

3 Comments

could not follow your SQL example? what you are trying to relate it with?
I'm just saying that I've tried queries before where I'm searching for null values, but have received none, because the word "null" was in the column where I was expecting a null value. This is why I discourage using string.valueOf for placing the result into a database table.
I didn't realize that another user could directly edit my comment here. This seems like an issue, given the sentiments in the comment get attributed to me, rather than who edited it. Sure you can look at the edits, but from first glance it appears I wrote the comment, though it was change rather significantly from my original comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.