I always thought that primitive types in Java cannot be null
, as it is a compile time error if i attempt to do something like this:
我一直认为Java中的原始类型不能为空,因为如果我尝试这样做的话,这是一个编译时错误:
int test = null;
However in a ternary operation, it seems to be allowed:
但在三元手术中,似乎可以:
int test = something != 0 ? 5 : null;
Isn't a ternary operation just short for (in this case):
(在本例中)不是三元操作(在本例中):
int test;
if (something != 0){
test = 5;
} else {
test = null
}
which of course should not be allowed. if that condition fails, It will automaticly throw a NullPointerException
due to autoboxing. So why the java-compiler doesn't fetch nonsense like this?
当然,这是不允许的。如果这个条件失败,它将自动抛出一个NullPointerException由于自动装箱。那么为什么java编译器不像这样取无意义的东西呢?
1 个解决方案
#1
12
What happens is that the Java compiler first tries to make the types of the expressions on either side of the :
equal. In this case, it autoboxes the 5
to an Integer
; note that null
is a valid value for Integer
. The result of the whole ternary expression is Integer
. You assign that to an int
, so the Integer
is then autounboxed.
发生的情况是,Java编译器首先尝试使:等号两边的表达式的类型相等。在这种情况下,它将5自动填入整数;注意,null是整型的有效值。整个三元表达式的结果是整数。你把它赋给一个整数,那么这个整数就是autounbox。
Essentially the compiler applies autoboxing and -unboxing so that the line is going to look like this:
本质上,编译器应用自动装箱和-反装箱,这样行就会是这样的:
int test = (something != 0 ? Integer.valueOf(5) : null).intValue();
Indeed, autounboxing null
leads to a NullPointerException
.
实际上,autounboxing null会导致NullPointerException。
So why the java-compiler doesn't fetch nonsense like this?
那么为什么java编译器不像这样取无意义的东西呢?
Because the designers of the Java language defined the language in such a way that it works like this and didn't decide that this has to be treated as an error...
因为Java语言的设计者定义语言的方式是这样的,它的工作方式是这样的,而不是认为必须将其视为一个错误……
Section 15.25 of the Java Language Specification explains how the type of the whole expression is determined.
Java语言规范的15.25节解释了如何确定整个表达式的类型。
#1
12
What happens is that the Java compiler first tries to make the types of the expressions on either side of the :
equal. In this case, it autoboxes the 5
to an Integer
; note that null
is a valid value for Integer
. The result of the whole ternary expression is Integer
. You assign that to an int
, so the Integer
is then autounboxed.
发生的情况是,Java编译器首先尝试使:等号两边的表达式的类型相等。在这种情况下,它将5自动填入整数;注意,null是整型的有效值。整个三元表达式的结果是整数。你把它赋给一个整数,那么这个整数就是autounbox。
Essentially the compiler applies autoboxing and -unboxing so that the line is going to look like this:
本质上,编译器应用自动装箱和-反装箱,这样行就会是这样的:
int test = (something != 0 ? Integer.valueOf(5) : null).intValue();
Indeed, autounboxing null
leads to a NullPointerException
.
实际上,autounboxing null会导致NullPointerException。
So why the java-compiler doesn't fetch nonsense like this?
那么为什么java编译器不像这样取无意义的东西呢?
Because the designers of the Java language defined the language in such a way that it works like this and didn't decide that this has to be treated as an error...
因为Java语言的设计者定义语言的方式是这样的,它的工作方式是这样的,而不是认为必须将其视为一个错误……
Section 15.25 of the Java Language Specification explains how the type of the whole expression is determined.
Java语言规范的15.25节解释了如何确定整个表达式的类型。