Java Keyword Switch

The keyword switch is used in conjunction with case keyword that forms the switch-case statement. The switch statement evaluates a variable and matches its value to a specific case parameter. If no case match is found, an optional label called default is then executed.

The switch statement must be used with the case statement to form the switch-case block.

The code sample below shows you how to use both the switch and case statements in a simple Java program.

// Sample

switch(jcdNumber) {

    case 1:
        System.out.println("Wow we got a 1. Let's party!");
    case 2: 
        System.out.println("Wow we got a 2. Sweet!");
    case 3: 
        System.out.println("Wow we got a 3. Awesome!");
        //Encountered something unexpected

While the default is not required in the switch-case block, I recommend you always include it so that you can account for something that is unexpected.

There is a list of 50 or 53 Java Reserved Keywords that are unable to be used as variable names. It’s 53 if you include the 3 literal words you also can’t use for variable names.

Add a Comment

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