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

后端 未结 9 1084
囚心锁ツ
囚心锁ツ 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:09

    The answer is simply that if you use a switch with a reference type (such as a boxed primitive type), the run-time error will occur if the expression is null because unboxing it would throw the NPE.

    so case null (which is illegal) could never be executed anyway ;)

提交回复
热议问题