Java

What Is Default Keyword In Java

The default keyword designates a default scenario inside a switch block.

Example of default keyword in switch block

This executes if a matching case cannot be identified and is used to provide a default case.

  • It doesn’t need a break statement because it is at the conclusion of the switch block.
  • The default case is skipped if a matching case is discovered.
  • We are showing how to utilize the default keyword in a switch-case block in the example below.
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

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button