The default keyword is used inside switch block to mark a default case.
Example of default keyword in switch block
- Used to specify a default case, this runs when no matching case is being found.
- It is placed in the end of the switch block and it doesn’t require break statement.
- If there is matching case found then the default case doesn’t execute.
In the following example, we are demonstrating the use of default keyword in switch-case block.
public class JavaExample {
public static void main(String[] args) {
int num = 15;
switch (num) {
case 10:
System.out.println("Execute this if num is 10");
break;
case 11:
System.out.println("Execute this if num is 11");
break;
default:
System.out.println("Execute this if num is neither 10 or 11");
}
}
}
Output:
Execute this if num is neither 10 or 11