How to use null in switch

前端 未结 12 979
南方客
南方客 2020-12-04 10:17
Integer i = ...

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

In the code above I cant use null in switch case state

相关标签:
12条回答
  • 2020-12-04 11:02

    You can also use String.valueOf((Object) nullableString) like

    switch (String.valueOf((Object) nullableString)) {
    case "someCase"
        //...
        break;
    ...
    case "null": // or default:
        //...
            break;
    }
    

    See interesting SO Q/A: Why does String.valueOf(null) throw a NullPointerException

    0 讨论(0)
  • 2020-12-04 11:02

    You can't. You can use primitives (int, char, short, byte) and String (Strings in java 7 only) in switch. primitives can't be null.
    Check i in separate condition before switch.

    0 讨论(0)
  • 2020-12-04 11:05

    Given:

    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;
        }
    }
    

    For me, this typically aligns with a look-up table in a database (for rarely-updated tables only).

    However, when I try to use findByTypeId in a switch statement (from, most likely, user input)...

    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.
    }
    

    ...as others have stated, this results in an NPE @ switch(personType) {. One work-around (i.e., "solution") I started implementing was to add an UNKNOWN(-1) type.

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

    Now, you don't have to do null-checking where it counts and you can choose to, or not to, handle UNKNOWN types. (NOTE: -1 is an unlikely identifier in a business scenario, but obviously choose something that makes sense for your use-case).

    0 讨论(0)
  • 2020-12-04 11:05

    You have to make a

    if (i == null) {
       doSomething0();
    } else {
       switch (i) {
       }
    }
    
    0 讨论(0)
  • 2020-12-04 11:05

    Some libraries attempt to offer alternatives to the builtin java switch statement. Vavr is one of them, they generalize it to pattern matching.

    Here is an example from their documentation:

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

    You can use any predicate, but they offer many of them out of the box, and $(null) is perfectly legal. I find this a more elegant solution than the alternatives, but this requires java8 and a dependency on the vavr library...

    0 讨论(0)
  • 2020-12-04 11:06

    Just consider how the SWITCH might work,

    • in case of primitives we know it can fail with NPE for auto-boxing
    • but for String or enum, it might be invoking equals method, which obviously needs a LHS value on which equals is being invoked. So, given no method can be invoked on a null, switch cant handle null.
    0 讨论(0)
提交回复
热议问题