-2

[edited this question at 2020/07/27 01:30 UTC]

What do you thínk about Java designers decided java.lang.String.valueOf((Object)null) returns "null" but not "" which is the length 0 text ?

I think that "" text is better than "null" text instead of null text.

I have already understood that null and "" are different, but sometimes "" and null means empty value on programs.

Actually, org.apache.commons.lang3.StringUtils.isEmpty() that returns true if value is "" or null. If String.valueOf(null) returns "", String.valueOf(null).isEmpty() returns true.

To avoid NullPointerException, we sometime use [] array instead of null array. new String((char)[]) returns "".

I think that a meaning of "zero" is "nothing", so length of null should be 0.

Alumuko
  • 171
  • 2
  • 11
  • 3
    Look at the code of the method. – Abra Jul 26 '20 at 18:16
  • 2
    Or read the docs: `if the argument is null, then a string equal to "null" [is returned]; otherwise, the value of obj.toString() is returned.` https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html#valueOf(java.lang.Object) – markspace Jul 26 '20 at 18:20
  • 1
    @Abra Question isn't "how" but "why" (in sense now "how code works" but "why Java *designers decided* it to act this way'). – Pshemo Jul 26 '20 at 18:21
  • One answer is "why" is because the docs (the external specification) says so. If you're looking to peer into the mind of the language designers, that's harder. I'd guess that "" is hard to distinguish in a print out of many objects and "null" was deemed better and easier to read. – markspace Jul 26 '20 at 18:23
  • 1
    Ask the person who wrote the method **why** she decided to write it that way. Null is **not** the same as an empty, i.e. zero length, string. – Abra Jul 26 '20 at 18:23
  • Yes. I'm sorry my poor English – Alumuko Jul 26 '20 at 18:24
  • I imagine it’s because when someone prints an object, they probably want to know if it’s null. Empty strings are common, and thus don’t distinguish null objects all that well; but the four letters “null” are not common. – VGR Jul 26 '20 at 21:35

2 Answers2

1

It's consistent with calling Object.toString(null).

Why should that specific result matter? I'd argue that when converting an object to a String, it's more generally useful to get "null" rather than an empty string.

For example, if you call

System.out.printf("the value is %s", obj);

where obj is null, then it seems more useful in general to see

the value is null

rather than

the value is

If you want something different, you can code valueOf(obj != null ? obj : ""). Of course, the same argument could have been made for the reverse decision - that you could have been made to write valueOf(obj != null ? obj : "null"). It thus boils down to judgement of which is the more common need.

user13784117
  • 1,124
  • 4
  • 4
  • Small nitpick: at "It's consistent with calling Object.toString(null)." you probably meant `Objects.toString` (typo, no `s` before `.toString`). But since `Objects.toString` internally just `return String.valueOf(o);` it is probably more accurate to say that `Objects.toString` is consistent with `String.valueOf`. – Pshemo Jul 26 '20 at 18:56
  • The String.valueOf methods are much, much older than the Objects class. – VGR Jul 26 '20 at 21:37
1

"null" is completely different from "". null means that you never set the value of a variable while when you have a variable with "" value it means you have once set that variable to something, even if that value is "".

some tips:

Tony Hoare introduced null reference while designing ALGOL and many languages after that have followed that approach but Hoare called it "my billion dollar mistake", many languages don't use that any more because of its pitfalls. Java 8 has introduced Optional type. to tackle these problems use that instead.

Tashkhisi
  • 2,070
  • 1
  • 7
  • 20