Java Switch Expression
public class Test {
public static void main(String[] args) {
System.out.println(countryName("CN"));
System.out.println(countryName("EN"));
System.out.println(countryName("US"));
System.out.println(countryName("XX"));
}
private static String countryName(String shortName) {
return switch(shortName) {
case "CN" -> "China";
case "EN" -> "English";
case "US" -> {
yield "United States";
}
default -> "Unknown";
};
}
}
Output:
China
English
United States
Unknown