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

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


当前回答

switch (String.valueOf(value)){
    case "null":
    default: 
}

其他回答

你不能。你可以在switch中使用基本类型(int, char, short, byte)和String(仅在java 7中使用字符串)。原语不能为空。 开关前检查i是否处于单独状态。

带开关的模式匹配

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

考虑到:

public enum PersonType {
    COOL_GUY(1),
    JERK(2);

    private final int typeId;
    private PersonType(int typeId) {
        this.typeId = typeId;
    }

    public final int getTypeId() {
        return typeId;
    }

    public static PersonType findByTypeId(int typeId) {
        for (PersonType type : values()) {
            if (type.typeId == typeId) {
                return type;
            }
        }
        return null;
    }
}

对我来说,这通常与数据库中的查找表(仅针对很少更新的表)保持一致。

然而,当我试图使用findByTypeId在一个开关语句(从,很可能,用户输入)…

int userInput = 3;
PersonType personType = PersonType.findByTypeId(userInput);
switch(personType) {
case COOL_GUY:
    // Do things only a cool guy would do.
    break;
case JERK:
    // Push back. Don't enable him.
    break;
default:
    // I don't know or care what to do with this mess.
}

…正如其他人所述,这将导致NPE @交换机(personType){。我开始实现的一个变通方法(即“解决方案”)是添加一个UNKNOWN(-1)类型。

public enum PersonType {
    UNKNOWN(-1),
    COOL_GUY(1),
    JERK(2);
    ...
    public static PersonType findByTypeId(int id) {
        ...
        return UNKNOWN;
    }
}

现在,您不必在重要的地方进行空检查,并且可以选择是否处理UNKNOWN类型。(注意:-1在业务场景中不太可能是标识符,但显然要选择对您的用例有意义的标识符)。

你必须做出一个

if (i == null) {
   doSomething0();
} else {
   switch (i) {
   }
}

Java文档明确指出:

禁止使用null作为开关标签可以防止编写永远不能执行的代码。如果开关表达式是引用类型,例如盒装基元类型或enum,如果表达式在运行时求值为空,则会发生运行时错误。

在switch语句执行之前,必须验证是否为空。

if (i == null)

参见Switch语句

case null: // will never be executed, therefore disallowed.