Starting from Java 7 release you can now use a String
in the switch
statement. On the previous version we can only use constants type of byte
, char
, short
, int
(and their corresponding reference / wrapper type) or enum
constants in the switch
statement.
The code below give you a simple example on how the Java 7 extended to allow the use of String
in switch
statement.
package org.kodejava.basic;
public class StringInSwitchExample {
public static void main(String[] args) {
String day = "Sunday";
switch (day) {
case "Sunday":
System.out.println("doSomething");
break;
case "Monday":
System.out.println("doSomethingElse");
break;
case "Tuesday":
case "Wednesday":
System.out.println("doSomeOtherThings");
break;
default:
System.out.println("doDefault");
break;
}
}
}
Latest posts by Wayan (see all)
- How do I create a table with multiple header in iText 8? - March 16, 2024
- How do I add Image to a Table in iText 8? - March 11, 2024
- How do I downgrade Android SDK emulator version? - February 12, 2024