Skip to main content
1 of 4
Prashant Bhate
  • 11.2k
  • 9
  • 51
  • 84

Why String switch statment does not support null case?

I am just wondering why Java 7 switch statement does not support null case and instead throws NPE ? see commented line below (example taken from http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html )

 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 from every switch use

Prashant Bhate
  • 11.2k
  • 9
  • 51
  • 84