Integer i = ...
    
switch (i) {
    case null:
        doSomething0();
        break;    
}

在上面的代码中,我不能在switch case语句中使用null。我该怎么做呢?我不能用默认,因为我想做别的。


当前回答

基于@tetsuo的答案,java 8:

Integer i = ...

switch (Optional.ofNullable(i).orElse(DEFAULT_VALUE)) {
    case DEFAULT_VALUE:
        doDefault();
        break;    
}

其他回答

我今天才知道,你不必放另一层缩进/花括号,只是因为if检查为空。你可以做以下任何一种:

if (i == null) {
    // ...
} else switch (i) {
    case 1:
        // ...
        break;
    default:
        // ...
}

or

if (i != null) switch (i) {
    case 1:
        // ...
        break;
    default:
        // ...
} else {
    // ...
}

带开关的模式匹配

switch语句的null行为将随着模式匹配的加入而改变(在JDK 17/18/19/20中有预览状态)

Switches and null Traditionally, switch statements and expressions throw NullPointerException if the selector expression evaluates to null, so testing for null must be done outside of the switch. [...] This was reasonable when switch supported only a few reference types. However, if switch allows a selector expression of any type, and case labels can have type patterns, then the standalone null test feels like an arbitrary distinction, and invites needless boilerplate and opportunity for error. It would be better to integrate the null test into the switch by allowing a new null case label.

参见JEP 433:模式匹配开关(第四预览)

这意味着基本上你可以简单地写

switch (null) {
case null: ...
}

但如果省略大小写null:-part,开关仍然会抛出NullPointerException

一些库尝试提供内置java switch语句的替代方案。Vavr就是其中之一,他们把它推广到模式匹配。

以下是他们文档中的一个例子:

String s = Match(i).of(
    Case($(1), "one"),
    Case($(2), "two"),
    Case($(), "?")
);

您可以使用任何谓词,但它们提供了许多开箱即用的谓词,$(null)是完全合法的。我发现这是一个比替代方案更优雅的解决方案,但这需要java8和对vavr库的依赖…

基于@tetsuo的答案,java 8:

Integer i = ...

switch (Optional.ofNullable(i).orElse(DEFAULT_VALUE)) {
    case DEFAULT_VALUE:
        doDefault();
        break;    
}
switch (String.valueOf(value)){
    case "null":
    default: 
}