Skip to main content
4 of 4
added 18 characters in body
Prashant Bhate
  • 11.2k
  • 9
  • 51
  • 84

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

I am just wondering why the Java 7 switch statement does not support a null case and instead throws NullPointerException? See the commented line below (example taken from the Java Tutorials article on switch):

{ String month = null; switch (month) { case "january": monthNumber = 1; break; case "february": monthNumber = 2; break; case "march": monthNumber = 3; break; //case null: default: monthNumber = 0; break; } return monthNumber; } 

This would have avoided an if condition for null check before every switch use.

Prashant Bhate
  • 11.2k
  • 9
  • 51
  • 84