Java 7 Feature Switch in String - Java @ Desk

Wednesday, August 20, 2014

Java 7 Feature Switch in String

Java 7 Feature Switch in String

Switch statement worked on byte, short, char, and int primitive data types and enum. It took us about 16 years to realize that java.lang.String is also a constant and add it to the list of data types supported by a switch statement

The switch statement transfers control to one of several statements depending on the value of an expression.

The switch statement compares the String object in its expression with the expressions associated with each case label as if it were using the String.equals method; consequently, the comparison of String objects in switch statements is case sensitive.

The Java compiler generates generally more efficient bytecode from switch statements that use String objects than from chained if-then-else statements.

All of the following must be true, or a compile-time error occurs:

1. Every case constant expression associated with a switch statement must be assignable to the type of the switch Expression. i.e a String switch cannot have case with expression value to be int or something else.
Type mismatch: cannot convert from int to String.


Note: The type of the Expression must be char, byte, short, int, Character, Byte,
Short, Integer, String, or an enum type, or a compile-time error occurs.


2. No two of the case constant expressions associated with a switch statement may have the same value.
Duplicate case error is thrown


3. No switch label can be null.
case expressions must be constant expressions


4. At most one default label may be associated with the same switch statement.
default case already defined


5. When the switch statement is executed, first the Expression is evaluated. If the Expression evaluates to null, a NullPointerException is thrown and the entire switch statement completes abruptly for that reason. Otherwise, if the result is of a reference type, it is subject to unboxing conversion

6. Empty String "" can be added as a case expression.

7. Case check are case sensitive

8. In order for the NSwitch example to accept any fruit regardless of case, fruit name is converted to lowercase (with the toLowerCase method), and all the strings associated with the case labels are in lowercase.

9. If evaluation of the Expression or the subsequent unboxing conversion (if any) completes abruptly for some reason, the switch statement completes abruptly for the same reason. Otherwise, execution continues by comparing the value of the Expression with each case constant, and there is a choice:

9.1. If one of the case constants is equal to the value of the expression, then we say
that the case matches, and all statements after the matching case label in the switch
block, if any, are executed in sequence.

9.2. If all these statements complete normally, or if there are no statements after
the matching case label, then the entire switch statement completes normally.

9.3. If no case matches but there are a default label, then all statements after the
matching default label in the switch block, if any, are executed in sequence.

9.4. If all these statements complete normally, or if there are no statements after the
default label, then the entire switch statement completes normally.

9.5. If no case matches and there is no default label, then no further action is taken
and the switch statement completes normally.




public class NSwitch {
 
  public static String getColor(String name) {
          if(name== null){
   return "Unknown";
   }   
switch (name.toLowerCase()) {
   case "apple":
   case "cherry":
   case "strawberry":
   return "Red";
   case "banana":
   case "papaya":
   return "Yellow";
   case "kiwi":
   case "grapes":
   case "guava":
   return "Green";
   case "clementine":
   case "persimmon":
   return "Orange";
   default:
   return "Unknown";
   }
   
  } 

  public static void main(String[] args) {
  getColor("apple");
 }
}


This post is written by Dipika Mulchandani. She is a freelance writer, loves to explore latest features in Java technology.





No comments:

Post a Comment