Always perform a null check before the switch statement. In the above example if variable 'name' contains a null reference then you will get an NPE(Null Pointer Exception).

reference then you will get an NPE(Null Pointer Exception). Since change in case leads to totally different value in String, there should be a proper check on case of String values.





Behind the scenes

We all know that switch statements are alternatives to nested if-else statements. But they are far more optimitized then a simple set of if-else statements. Still I won't say using switch statements will always give the most optimized code. There are a number of ways that can help you to write a better code than simply using switch/case statements.

Switch statements. So in the below example I have used the technique that is used current implementation of this update.



Here we have used simple switch statements using integers. We all know that hashCode of any string gives an integer value. So we used 'hashCode of string' in switch since there is a support for switch between integers in previous versions of Java SE. Since I have mentioned earlier that the null check is must for the switch statements for Strings, name.hashCode() will lead to NPE if name is null. Now if the hashCode of the two strings is found equal, then it compares the both strings with equals method. And a valid result confirms that the two strings are equal and the instructions associated with that case gets executed. So now the question that arises is how is this feature implemented in Java SE 7. So it is nothing but the mixture of hashCode, equals() and the previous version ofstatements. So in the below example I have used the technique that is used current implementation of this update.





Advanced Example:

boolean fall_through = false; boolean defaultCase = false; String name; label outOfSwitch: { switch(name.hashCode()) { // This statement will lead to NPE in case name is null case "Saurab".hashCode(): // This statement will lead to NPE in case name is null if(!fall_through&&!name.equals("Saurab")) { /* This is the case where the hashCode is equal but still the string fails the equals method. So the statements for default case are executed*/ defaultCase = true; break outOfSwitch; } else { proceedAhead(); fall_through = true; // So now the code falls through to the next case. } case "Vishesh".hashCode(): if(!fall_through&&!name.equals("Vishesh")) { /* This is the case where the hashCode is equal but still the string fails the equals method. So the statements for default case are executed*/ defaultCase = true; break outOfSwitch; } else { proceedAhead(); fall_through = true; // So now the code falls through to the next case. } case "Shivang".hashCode(): if(!fall_through&&!name.equals("Shivang")) { /* This is the case where the hashCode is equal but still the string fails the equals method. So the statements for default case are executed*/ defaultCase = true; break outOfSwitch; } else { proceedAhead(); fall_through = true; // So now the code falls through to the next case. } default: defaultCase = true; break outOfSwitch; } } if(defaultCase) { /* Execute the default Case statements */ } In the above examples two flags are maintained (fall_through and defaultCase). So if the fall_through gets true in any case means there is no need to execute the equals method of the remaining cases. And defaultCase gets true when it breaks out of switch statements and executes the instructions associated with the default case.



Next Feature: Improved and Better Exception Handling from Java 7

The latest update to java i.e Java 7 which was launched a few days back. So as a developer , I was pretty curious to know about the changes made with this version of update. Since this update included mostly enhancements and bug fixes, here is the list of changes that I found useful.A very essential feature that was in demand for the last two decades is finally introduced in Java SE 7. So it is possible to use switch/case statements foralong with the previously acceptedandA very basic example to understand the use of switch with Strings.Example:So this was a simple example of the new Java SE 7 feature. In the previous version you would get a compile time exception("Incompatible Types. Require int instead of String") for the code written above.Things to keep in mind while using switch with Strings.