What do you thínk about Java designers decided java.lang.String.valueOf((Object)null) returns "null" but not ""

huangapple go评论65阅读模式
英文:

What do you thínk about Java designers decided java.lang.String.valueOf((Object)null) returns "null" but not ""

问题

[在 2020 年 07 月 27 日 01:30 UTC 对这个问题进行了编辑]

你认为 Java 的设计者决定 java.lang.String.valueOf((Object)null) 返回 "null" 而不是长度为 0 的空文本 "",你有什么想法?

我认为 "" 文本比 "null" 文本更好,而不是 null 文本。

我已经理解 null"" 是不同的,但有时候在程序中 ""null 都表示空值。

实际上,org.apache.commons.lang3.StringUtils.isEmpty() 会在值为 ""null 时返回 true。
如果 String.valueOf(null) 返回 "",那么 String.valueOf(null).isEmpty() 会返回 true

为了避免空指针异常,有时候我们会使用 [] 数组来代替空数组。new String((char)[]) 会返回 ""

我认为 "零" 的意思是 "没有任何东西",所以 null 的长度应该是 0。

英文:

[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.

答案1

得分: 1

这与调用 Object.toString(null) 是一致的。

为什么这个特定的结果很重要呢?我会认为,在将对象转换为字符串时,通常情况下得到 "null" 要比得到一个空字符串更有用。

例如,如果你调用

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

其中 obj 为 null,那么通常情况下看到

the value is null

要比看到

the value is

更有用。如果你想要不同的结果,你可以编写 valueOf(obj != null ? obj : "")。当然,同样的论点也可以用于相反的决定 - 你本可以被要求编写 valueOf(obj != null ? obj : "null")。因此,这归结为判断哪种情况更常见。

英文:

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.

答案2

得分: 1

"null"与""完全不同。null表示您从未设置变量的值,而当您的变量具有""值时,这意味着您曾经将该变量设置为某个值,即使该值为""。

一些建议:

Tony Hoare在设计ALGOL时引入了空引用,之后许多语言都采用了这种方法,但是Hoare将其称为“我的十亿美元错误”,由于其陷阱,许多语言不再使用它。 Java 8引入了Optional类型,以解决这些问题,请改用它。

英文:

"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.

huangapple
  • 本文由 发表于 2020年7月27日 02:14:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/63103953.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定