为什么String switch语句不支持null case? [英] Why doesn't String switch statement support a null case?
问题描述
我只是想知道为什么Java 7 switch
语句不支持 null
的情况而是抛出的NullPointerException
?请参阅下面的注释行(示例来自关于<$ c的Java教程文章$ c>开关 ):
I am just wondering why the Java 7 switch
statement does not support a null
case and instead throws NullPointerException
? See the commented line below (example taken from the Java Tutorials article on switch
):
{
String month = null;
switch (month) {
case "january":
monthNumber = 1;
break;
case "february":
monthNumber = 2;
break;
case "march":
monthNumber = 3;
break;
//case null:
default:
monthNumber = 0;
break;
}
return monthNumber;
}
如果$ c,这将避免 $ c>在每个
开关
之前进行空检查的条件。
This would have avoided an if
condition for null check before every switch
use.
推荐答案
正如damryfbfnetsi在评论中指出 JLS§14.11有以下注释:
As damryfbfnetsi points out in the comments, JLS §14.11 has the following note:
禁止使用
null
作为开关标签阻止来自编写永远不会执行的代码。如果开关
表达式是引用类型,即String
或盒装基元类型或枚举类型,如果表达式在运行时计算为null
,则会发生运行时错误。 在Java编程语言的设计者的判断中,这比静默跳过整个开关
语句或选择执行语句(如果有的话)更好在默认
标签后(如果有)。
The prohibition against using
null
as a switch label prevents one from writing code that can never be executed. If theswitch
expression is of a reference type, that is,String
or a boxed primitive type or an enum type, then a run-time error will occur if the expression evaluates tonull
at run time. In the judgment of the designers of the Java programming language, this is a better outcome than silently skipping the entireswitch
statement or choosing to execute the statements (if any) after thedefault
label (if any).
(强调我的)
虽然最后一句话超过了使用 case null:
的可能性,但这似乎是合理的并且提供了语言设计者意图的观点。
While the last sentence skips over the possibility of using case null:
, it seems reasonable and offers a view into the language designers' intentions.
如果我们宁愿查看实施细节,这篇博客文章对于为什么交换机中不允许 null
有一些深刻的猜测(虽然它以 enum
开关为中心,而不是 String
开关):
If we rather look at implementation details, this blog post by Christian Hujer has some insightful speculation about why null
isn't allowed in switches (although it centers on the enum
switch rather than the String
switch):
在引擎盖下,
开关
语句通常会编译为tablesswitch字节代码。switch
的物理参数及其案例是int
s。要打开的int值是通过调用方法Enum.ordinal()
来确定的。 [...]序数从零开始。
Under the hood, the
switch
statement will typically compile to a tablesswitch byte code. And the "physical" argument toswitch
as well as its cases areint
s. The int value to switch on is determined by invoking the methodEnum.ordinal()
. The [...] ordinals start at zero.
这意味着,将 null
映射到 0
不是一个好主意。第一个枚举值的开关将与null无法区分。也许以1开始计算枚举的序数是个好主意。但是它没有像那样定义,并且这个定义不能改变。
That means, mapping null
to 0
wouldn't be a good idea. A switch on the first enum value would be indistinguishible from null. Maybe it would've been a good idea to start counting the ordinals for enums at 1. However it hasn't been defined like that, and this definition can not be changed.
虽然字符串
切换以不同方式实施, enum
开关是第一个,并设置了当引用 null $ c时引用类型应如何处理的先例$ c>。
While String
switches are implemented differently, the enum
switch came first and set the precedent for how switching on a reference type should behave when the reference is null
.
这篇关于为什么String switch语句不支持null case?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!