Java Default Keyword

Java Default Keyword


Default Keyword In Java
1. default is an optional part.
2. We can place the default anywhere inside the switch_case.
3. The switch compares the result of the integral selector to each integral value. If it finds a match, the corresponding statement (a single statement or multiple statements; braces are not required) executes. If no match occurs, the default statement executes.
4. You could put a break at the end of the default statement with no harm
Now we will see an example - 
Example 1- Here is a simple example that uses a default statement
// web designing house
// online promotion house
// A simple example of the switch.
class Main{public static void main(String args[]) {
for(int i=0; i<6; i++)
switch(i) {
case 0:
System.out.println("i is zero.");
break;
case 1:
System.out.println("i is one.");
break;
case 2:
System.out.println("i is two.");
break;
case 3:
System.out.println("i is three.");
break;
default:
System.out.println("i is greater than 3.");
}
}
}
Output - Output will be -
i is zero.
i is one.
i is two.
i is three.
i is greater than 3.
i is greater than 3.

 
Example 2 - Here’s an example that takes day number as an input, and prints day name as an output -
import java.util.*;
public class VowelsAndConsonants { public static void main(String[] args) {
int dayno;
System.out.println("Input Day No : ");
Scanner ob = new Scanner(System.in);
dayno = ob.nextInt();
  switch(dayno)
  {
case 1:
      System.out.println("Monday);
      break;
case 2:
      System.out.println("Tuesday ");
      break;
case 3:
      System.out.println("Wednesday ");
      break;
case 4:
      System.out.println("Thursday");
      break;
case 5:
      System.out.println("Friday ");
      break;
case 6:
  System.out.println("Saturday ");
      break;
case 7:
      System.out.println("Sunday ");
      break;
default:
      System.out.println("Invalid day number. \nPlease try again ....");
      break;
      }
}
 
Output-
 
Input Day No : 10                                                                                               
Invalid day number. \nPlease try again ....