Java 12 extends the switch statement so that it can be used either as a statement or as an expression (JEP 325).
This change is a preview language feature.
The new switch syntax can only be compiled (via javac) and run (via java) with --enable-preview flag.
Let's understand new switch syntax with examples.
Examples
New switch syntax as statement
A new form of switch label, written as "case L ->" has been introduced. If a label is matched, then only the statement (or expression) to the right of an arrow label is executed. Also there is no fall though.
package com.logicbig.example;
import java.time.LocalDate;
import java.time.Month;
public class SwitchStatementNewEx {
public static void main(String[] args) {
showQuarter(LocalDate.now().getMonth());
}
public static void showQuarter(Month month) {
switch (month) {
case JANUARY, FEBRUARY, MARCH -> System.out.println("First Quarter");//no break needed
case APRIL, MAY, JUNE -> System.out.println("Second Quarter");
case JULY, AUGUST, SEPTEMBER -> System.out.println("Third Quarter");
case OCTOBER, NOVEMBER, DECEMBER -> System.out.println("Forth Quarter");
default -> System.out.println("Unknown Quarter");
}
}
}
First Quarter
If we write above example in old traditional way:
package com.logicbig.example;
import java.time.LocalDate;
import java.time.Month;
public class SwitchStatementOldEx {
public static void main(String[] args) {
showQuarter(LocalDate.now().getMonth());
}
public static void showQuarter(Month month) {
switch (month) {
case JANUARY:
case FEBRUARY:
case MARCH:
System.out.println("First Quarter");
break;
case APRIL:
case MAY:
case JUNE:
System.out.println("Second Quarter");
break;
case JULY:
case AUGUST:
case SEPTEMBER:
System.out.println("Third Quarter");
break;
case OCTOBER:
case NOVEMBER:
case DECEMBER:
System.out.println("Forth Quarter");
default:
System.out.println("Unknown Quarter");
}
}
}
First Quarter
New switch syntax as expression
The switch block used as an expression can directly return a value or it can be assigned to a variable:
package com.logicbig.example;
import java.time.LocalDate;
import java.time.Month;
public class SwitchExpressionExample {
public static void main(String[] args) {
showQuarter(LocalDate.now().getMonth());
}
public static void showQuarter(Month month) {
String quarter = switch (month) {
case JANUARY, FEBRUARY, MARCH -> "First Quarter"; //must be a single returning value
case APRIL, MAY, JUNE -> "Second Quarter";
case JULY, AUGUST, SEPTEMBER -> "Third Quarter";
case OCTOBER, NOVEMBER, DECEMBER -> "Forth Quarter";
default -> "Unknown Quarter";
};
System.out.println(quarter);
}
}
First Quarter
If we write above example in old traditional way:
package com.logicbig.example;
import java.time.LocalDate;
import java.time.Month;
public class SwitchExpressionOldExample {
public static void main(String[] args) {
showQuarter(LocalDate.now().getMonth());
}
public static void showQuarter(Month month) {
String quarter;
switch (month) {
case JANUARY:
case FEBRUARY:
case MARCH:
quarter = "First Quarter";
break;
case APRIL:
case MAY:
case JUNE:
quarter = "Second Quarter";
break;
case JULY:
case AUGUST:
case SEPTEMBER:
quarter = "Third Quarter";
break;
case OCTOBER:
case NOVEMBER:
case DECEMBER:
quarter = "Forth Quarter";
break;
default:
quarter = "Unknown Quarter";
}
System.out.println(quarter);
}
}
First Quarter
New switch expression with break
The new switch expressions with arrow syntax can only be used with a single value expression to the right of "case L ->" (last example). In cases where full block is needed, a new syntax with break statement is introduced. A switch expression can now use a "traditional" switch block with colons ("case L:"). In this case values are returned using the 'break' along with an argument:
package com.logicbig.example;
import java.time.LocalDate;
import java.time.Month;
public class SwitchExpressionWithBreak {
public static void main(String[] args) {
showQuarter(LocalDate.now().getMonth());
}
public static void showQuarter(Month month) {
String result = switch (month) {
case JANUARY:
case FEBRUARY:
case MARCH:
//multiple statements can be used here
break "First Quarter";
case APRIL:
case MAY:
case JUNE:
break "Second Quarter";
case JULY:
case AUGUST:
case SEPTEMBER:
break "Third Quarter";
case OCTOBER:
case NOVEMBER:
case DECEMBER:
break "Forth Quarter";
default:
break "Unknown Quarter";
};
System.out.println(result);
}
}
First Quarter
IDE support
IntelliJ IDEA 2019.1 (early access - next version) supports Java 12 switch statements.
Example ProjectDependencies and Technologies Used: |