Week 47 — What is a switch expression and how is it different from switch statements?

Question of the Week #47
What is a switch expression and how is it different from switch statements?
4 Replies
Eric McIntyre
Eric McIntyre13mo ago
The switch statement allows to execute different code depending on the value of an expression:
enum SomeEnum{
A,B,C,D,E
}
enum SomeEnum{
A,B,C,D,E
}
SomeEnum value = SomeEnum.B;//some value

switch(value){
case A -> System.out.println("a");
case B -> System.out.println("b");
case C -> System.out.println("c");
case D -> System.out.println("d");
//No case for SomeEnum.E here
}
SomeEnum value = SomeEnum.B;//some value

switch(value){
case A -> System.out.println("a");
case B -> System.out.println("b");
case C -> System.out.println("c");
case D -> System.out.println("d");
//No case for SomeEnum.E here
}
This code prints different text depending on what is stored inside value without printing anything for E. If we instead wanted to store text in a variable, we would need to assign that variable in every case:
SomeEnum value = SomeEnum.B;//some value

String result = "";
switch(value){
case A -> result = "a";
case B -> result = "b";
case C -> result = "c";
case D -> result = "d";
//No case for SomeEnum.E here
}
System.out.println(result);
SomeEnum value = SomeEnum.B;//some value

String result = "";
switch(value){
case A -> result = "a";
case B -> result = "b";
case C -> result = "c";
case D -> result = "d";
//No case for SomeEnum.E here
}
System.out.println(result);
Eric McIntyre
Eric McIntyre13mo ago
With switch expressions, it is possible to assign give back a value in each case and then use the result of the combined switch expression somewhere else. However, that requires exhaustiveness i.e. every possible case needs to be matched:
SomeEnum value = SomeEnum.B;//some value

String result = switch(value){
case A -> "a";
case B -> "b";
case C -> "c";
case D -> "d";
case E -> "";//if this case was missing, there would be a compiler error
}
System.out.println(result);
SomeEnum value = SomeEnum.B;//some value

String result = switch(value){
case A -> "a";
case B -> "b";
case C -> "c";
case D -> "d";
case E -> "";//if this case was missing, there would be a compiler error
}
System.out.println(result);
The result of the switch expression is assigned to the variable result and the compiler checks that a value exists in every case.
📖 Sample answer from dan1st
Eric McIntyre
Eric McIntyre13mo ago
Switch expression evaluates to a value and can be used in switch statements but switch statements are the executes statements from multiple conditions.Switch statements are also called switch case. Let's explain this with an example: Day day = Day.WEDNESDAY;
System.out.println( switch (day) { case MONDAY, FRIDAY, SUNDAY -> 6; case TUESDAY -> 7; case THURSDAY, SATURDAY -> 8; case WEDNESDAY -> 9; default -> throw new IllegalStateException("Invalid day: " + day); } ); The above code block is a switch expression that uses the new kind of case label to print the number of letters of a day of the week.
Eric McIntyre
Eric McIntyre13mo ago
Switch statement with example:public enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY; } // ... int numLetters = 0; Day day = Day.WEDNESDAY; switch (day) { case MONDAY: case FRIDAY: case SUNDAY: numLetters = 6; break; case TUESDAY: numLetters = 7; break; case THURSDAY: case SATURDAY: numLetters = 8; break; case WEDNESDAY: numLetters = 9; break; default: throw new IllegalStateException("Invalid day: " + day); } System.out.println(numLetters);
Submission from dasa_51082
Want results from more Discord servers?
Add your server