为什么 java switch 语句不能处理空值,因为它有一个“默认”子句?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19931408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why java switch statement can't handle null, since it has a "default" clause?
提问by
Why java switch statement can't handle null, since it has a "default" clause?
为什么 java switch 语句不能处理空值,因为它有一个“默认”子句?
For example, if you have something like
例如,如果你有类似的东西
switch(value){
case VAL1: do_something1(); break;
case VAL2: do_something2(); break;
default: do_something3();
}
shouldn't "default" deal with any other value, such as null?
不应该“默认”处理任何其他值,例如 null?
采纳答案by Matt Ball
As always, it's in the JLS:
与往常一样,它在 JLS 中:
14.11. The
switch
StatementAll of the following must be true, or a compile-time error occurs:
- ...
- No switch label is
null
....
The prohibition against usingnull
as a switch label prevents one from writing code that can never be executed. If the switch 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).
14.11. 该
switch
声明以下所有条件都必须为真,否则会发生编译时错误:
- ...
- 没有开关标签是
null
。...
禁止null
用作开关标签可防止编写永远无法执行的代码。如果 switch 表达式是引用类型,即String
盒装原始类型或枚举类型,那么如果表达式null
在运行时计算为,则会发生运行时错误。在 Java 编程语言的设计者的判断中,这比默默地跳过整个switch
语句或选择执行default
标签(如果有)之后的语句(如果有)更好的结果。
回答by davek
You can't SWITCH
on strings and other data types that can be null
. This behaviour has been changed in Java 7 to allow string-switches.
你不能SWITCH
在字符串和其他可以是null
. 此行为已在 Java 7 中更改以允许字符串切换。
See this question for more info:
有关更多信息,请参阅此问题:
回答by Alex
This is by definition. I wouldn't lose too much time on this as it's in the specifications.
这是根据定义。我不会在这方面浪费太多时间,因为它在规范中。
On the other hand I find this quite practical in fact, as in case of being null it doesn' have a primitive type associated, so we can't know how to handle it properly.
另一方面,我发现这实际上非常实用,因为在为 null 的情况下,它没有关联的原始类型,因此我们不知道如何正确处理它。
回答by Marko Topolnik
In short, this design choice is in the spirit of Java.
简而言之,这种设计选择符合 Java的精神。
The decision to throw an NPE when a switch expression evaluates to null
is along the lines of other decisions made in Java, which always prefer throwing an exception to silently handling the null
in the "obvious" way. The general rule seems to be that, when in doubt, the Java designers choose the option which results in moreboilerplate code.
当 switch 表达式的计算结果为时抛出 NPE 的null
决定与 Java 中做出的其他决定一致,Java 总是更喜欢抛出异常,而不null
是以“明显”的方式静默处理。一般规则似乎是,当有疑问时,Java 设计者会选择导致更多样板代码的选项。
Some would call this frustrating and unfortunate (myself included), where others will religiously disagree, maintaining that this is a "safer" decision.
有些人会称这令人沮丧和不幸(包括我自己),而其他人会在宗教上不同意,认为这是一个“更安全”的决定。
For another frustrating example see the enchanced forloop, which also throws an NPE if the collection is null
, instead of acting as if the collection was empty. There are many more examples in the JDK library, where the caller must dilligently check all edge cases just to be allowed to treat them uniformly with the "normal" cases.
对于另一个令人沮丧的示例,请参阅增强的 for循环,如果集合为null
,它也会抛出 NPE ,而不是像集合为空一样。JDK 库中有更多示例,调用者必须勤奋地检查所有边缘情况,才能允许将它们与“正常”情况统一对待。
As a third notorious example, just look at the mess which the "language feature" of checked exceptions makes to your code.
作为第三个臭名昭著的例子,看看检查异常的“语言特性”给你的代码带来的混乱。