0
Why we have to use semicolon at the end of the switch in this program
public class Program { public static void main(String[] args) { int day = 2; String dayType = switch(day) { case 1, 2, 3, 4, 5 -> "Working day"; case 6, 7 -> "Weekend"; default -> "Invalid day"; };//Here System.out.println(dayType); } }
3 Answers
+ 3
because switch is declared in a variable
always after a variable declaration is needed ";"
+ 1
This is a Java switch expression, not a switch statement. Like other expressions, a value is returned once it has been evaluated. An expression is a rvalue and therefore requires a semicolon. This is similar to how an anonymous method/function vs a regular function is treated. Where the anonymous function is an rvalue and would similarly require a semicolon vs a regular function which is a class member.
https://docs.oracle.com/en/java/javase/13/language/switch-expressions.html
- 1
because it defines the end of a statement.
https://www.sololearn.com/discuss/1413209/?ref=app