为什么String switch语句不支持null case? [英] Why doesn't String switch statement support a null case?

查看:1852
本文介绍了为什么String switch语句不支持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>在每个开关之前进行空检查的条件。

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 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 to null at run time. In the judgment of the designers of the Java programming language, this is a better outcome than silently skipping the entire switch statement or choosing to execute the statements (if any) after the default 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 to switch as well as its cases are ints. The int value to switch on is determined by invoking the method Enum.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

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆