Why "null" String is returned in String.valueOf instead of java null?

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

The call

Objects.toString(obj, null)

returns null when object is null.


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


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.

Tags:

Java