Java 14 has promoted Java 12 and Java 13 switch improvements to standard feature.
Examples
Switch Statement
package com.logicbig.example;
import java.time.LocalDate;
import java.time.Month;
public class SwitchStatementExample {
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
Switch Expression
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
Switch Expression with yield
In last switch expression example a single expression to the right of the "case L ->" switch label was used. If a full block is needed, new keyword yield can be used as shown.
package com.logicbig.example;
import java.time.LocalDate;
import java.time.Month;
public class SwitchExpressionWithYieldAndArrow {
public static void main(String[] args) {
showQuarter(LocalDate.now().getMonth());
}
public static void showQuarter(Month month) {
String result = switch (month) {
case JANUARY,
FEBRUARY,
MARCH -> {
//multiple statements can be used here
yield "First Quarter";
}
case APRIL, MAY, JUNE -> {
//multiple statements can be used here
yield "Second Quarter";
}
case JULY, AUGUST, SEPTEMBER -> "Third Quarter";
case OCTOBER, NOVEMBER, DECEMBER -> {
//multiple statements can be used here
yield "Forth Quarter";
}
default -> "Unknown Quarter";
};
System.out.println(result);
}
}
First Quarter
Using yield with traditional switch block
A switch expression can, like a switch statement, also use a traditional switch block:
package com.logicbig.example;
import java.time.LocalDate;
import java.time.Month;
public class SwitchExpressionWithYield {
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
yield "First Quarter";
case APRIL:
case MAY:
case JUNE:
//multiple statements can be used here
yield "Second Quarter";
case JULY:
case AUGUST:
case SEPTEMBER:
//multiple statements can be used here
yield "Third Quarter";
case OCTOBER:
case NOVEMBER:
case DECEMBER:
//multiple statements can be used here
yield "Forth Quarter";
default:
yield "Unknown Quarter";
};
System.out.println(result);
}
}
First Quarter
Example ProjectDependencies and Technologies Used: |