为什么String.valueOf(null)会抛出NullPointerException? [英] Why does String.valueOf(null) throw a NullPointerException?
问题描述
根据文档,方法 String.valueOf(Object obj)
返回:
according to the documentation, the method String.valueOf(Object obj)
returns:
如果参数是
null
,则字符串等于null
;否则,返回obj.toString()
的值。
if the argument is
null
, then a string equal to"null"
; otherwise, the value ofobj.toString()
is returned.
但是当我尝试这样做时:
But how come when I try do this:
System.out.println("String.valueOf(null) = " + String.valueOf(null));
它会引发NPE吗? (如果你不相信,请亲自尝试!)
it throws NPE instead? (try it yourself if you don't believe!)
Exception in thread "main" java.lang.NullPointerException
at java.lang.String.(Unknown Source)
at java.lang.String.valueOf(Unknown Source)
为什么这发生了什么?文档对我说谎吗?这是Java中的一个主要错误吗?
How come this is happening? Is the documentation lying to me? Is this a major bug in Java?
推荐答案
问题在于 String.valueOf
方法重载:
String.valueOf(Object)
String.valueOf(char[])
Java规范语言要求在这种情况下,选择最具体的重载:
Java Specification Language mandates that in these kind of cases, the most specific overload is chosen:
如果多个成员方法都可访问并适用于方法调用,则必须选择一个提供th用于运行时方法调度的e描述符。 Java编程语言使用选择最具体方法的规则。
A char []
is-an Object
,但不是全部 Object
是-a char []
。因此, char []
更具体而不是 Object
,并且由Java指定语言,在这种情况下选择 String.valueOf(char [])
重载。
A char[]
is-an Object
, but not all Object
is-a char[]
. Therefore, char[]
is more specific than Object
, and as specified by the Java language, the String.valueOf(char[])
overload is chosen in this case.
String.valueOf(char [])
期望数组为非 - null
,并且因为 null 在这种情况下给出code>,然后抛出
NullPointerException
。
String.valueOf(char[])
expects the array to be non-null
, and since null
is given in this case, it then throws NullPointerException
.
简单的修复是将 null
明确地转换为 Object
,如下所示:
The easy "fix" is to cast the null
explicitly to Object
as follows:
System.out.println(String.valueOf((Object) null));
// prints "null"
相关问题
- 多态模糊区分如何工作?
- 在Java中为null选择哪个重载?
- How does polymorph ambiguity distinction work?
- Which overload will get selected for null in Java?
- 有效的Java第2版,第41项:明智地使用重载
- 仅仅因为你可以超载,并不意味着你应该每次都
- 它们可能引起混淆(特别是如果方法做了截然不同的事情)
- 使用Eclipse,你可以鼠标悬停在上面的表达式上看到确实,选择
valueOf(char [])
重载!
- With Eclipse, you can mouse-hover on the above expression and see that indeed, the
valueOf(char[])
overload is selected!
- Polymorphism vs Overriding vs Overloading
- Method Overloading. Can you overuse it?
至少有两种情况明确地将
null
强制转换为特定的引用类型是必要的:There are at least two situations where explicitly casting
null
to a specific reference type is necessary:- 选择重载(如上例所示)
- 给予
null
作为vararg参数的单个参数
- To select overloading (as given in above example)
- To give
null
as a single argument to a vararg parameter
一个简单的例子后者如下:
A simple example of the latter is the following:
static void vararg(Object... os) { System.out.println(os.length); }
然后,我们可以拥有以下内容:
Then, we can have the following:
vararg(null, null, null); // prints "3" vararg(null, null); // prints "2" vararg(null); // throws NullPointerException! vararg((Object) null); // prints "1"
参见
- Java语言指南/ varargs - 了解它是如何实现的
- Java Language Guide/varargs - to understand how it's implemented
- Why null cast?
- Difference between double… and double[] in formal parameter type declaration
See also
这篇关于为什么String.valueOf(null)会抛出NullPointerException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
Related questions
有几个重要的: