Why doesn't String switch statement support a null case?

后端 未结 9 1142
囚心锁ツ
囚心锁ツ 2020-12-13 16:41

I am just wondering why the Java 7 switch statement does not support a null case and instead throws NullPointerException? See the comm

9条回答
  •  不知归路
    2020-12-13 17:30

    In general null is nasty to handle; maybe a better language can live without null.

    Your problem might be solved by

        switch(month==null?"":month)
        {
            ...
            //case "":
            default: 
                monthNumber = 0;
    
        }
    

提交回复
热议问题