Java Switch Case for String
A switch statement allows a variable to be tested for equality against a list of values.
A switch works with the byte, short, char, and int primitive data types. It also works with enumerated types.
The body of a switch statement is known as a switch block. A statement in the switch block can be labeled with one or more case or default labels. The switch statement evaluates its expression, then executes all statements that follow the matching case label.
Each break statement terminates the enclosing switch statement.
Control flow continues with the first statement following the switch block.
The break statements are necessary because without them, statements in switch blocks fall through: All statements after the matching case label are executed in sequence, regardless of the expression of subsequent case labels, until a break statement is encountered.
In the JDK 7 release, you can use a String object in the expression of a switch statement:
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.
Important Points Java Switch Case for String
- Switching on strings can be more expensive in term of execution than switching on primitive data types. Therefore, it is best to switch on strings only in cases in which the controlling data is already in string form.
- The Java compiler generates generally more efficient bytecode from switch statements that use String objects than from chained if-then-else statements
- We should ensure while using a switch case for string, we should not pass null value else a NullPointerException being thrown at run-time.
- The switch statement for string 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.
Implementation in JDK 7 for Switch Case for String
A switch with String cases is translated into two switches during compilation. The first maps each string to a unique integer—its position in the original switch.
This is done by first switching on the hash code of the label.
The corresponding case is an if statement that tests string equality; if there are collisions on the hash, the test is a cascading if-else-if. The second switch mirrors that in the original source code, but substitutes the case labels with their corresponding positions.
This two-step process makes it easy to preserve the flow control of the original switch.
Before JDK 7
Prior to JDK 7, the Enum could approximate a String-based switch. This uses the static valueOf method generated by the compiler on every enum type. For example:
String day = "MONDAY";
WEEKDAY p = WEEKDAY.valueOf(day);
switch(p) {
case MONDAY: break;
case TUESDAY: break;
case WEDNESDAY: break;
case THURSDAY: break;
case FRIDAY: break;
case SATURDAY: break;
case SUNDAY: break;
}
In JDK 7
public class Main {
public static void main(String[] args) {
String current = "
WEDNESDAY ";
Days currentDay = Days.valueOf(current.toUpperCase());
switch (currentDay) {
case MONDAY:
System.out.println("
good day ");
case TUESDAY:
System.out.println("
average day ");
case WEDNESDAY:
System.out.println("
bore day ");
break;
case THURSDAY:
System.out.println("
getting better ");
case FRIDAY:
System.out.println(" End of work week
better ");
case SATURDAY:
case SUNDAY:
System.out.println("
much better ");
break;
default:
throw new IllegalArgumentException("Invalid day of the week: " + dayOfWeekArg);
}
}
}
public enum Days {
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY,
SUNDAY
}
}
output:
bore day
The String in the switch expression is compared with the expressions associated with each case label as if the String.equals method were being used.
Post/Questions related to Java Switch Case for String
Java adding New Line in a StringJava String split() method Examples
String lastIndexOf() method examples in Java
String indexOf() method example in Java
String array in Java
How to check if an enum contains String
StringBuffer in Java with Examples
In this article, we have seen Java Switch Case for String.
0 Comments
Post a Comment