JAVA TUTORIAL

Java Intro Java Features Java vs C++ Java Simple Program Java Applet vs Application JVM,JDK or JRE Java Keywords Java Variables Java Literals Java Separators Java Datatypes Java Operators Java Statements Java Array Java String

CONTROL STATEMENTS

Java If-else Java While Loop Java Do-While Loop Java For Loop Java For-each Loop Java Switch Java Break/Continue

JAVA METHODS

Java Method Java Overloading Java Overriding

JAVA CLASSES

Java OOPs Java Classes/Objects Java Inheritance Java Polymorphism Java Encapsulation Java Abstraction Java Modifiers Java Constructors Java Interface Java static keyword Java this keyword

Java Switch Statement

The java switch statement can have a number of possible execution paths. A switch works with the byte, short, char , and int primitive data types. It also works with enumerated types, the String class, and a few special classes that wrap certain primitive types: Character, Byte, Short, and Integer

In simple word switch case statement is used when we have number of options (or choices) and we may need to perform a different task for each choice.

Syntax:

switch(expression){
    case value1:
    // statement or code to be executed;
    break; // optional
    case value2:
    // statement or code to be executed;
    break; // optional
    case value3:
    // statement or code to be executed;
    break; // optional
    ...
    ...
    ...
    default:
    // if all cases are not matched then this code to be executed;
}

Some Important points/rule should be followed when we use switch statements:

1. Duplicate case values are not allowed.

2. The value for a case must be a literal or a constant. Variables are not allowed.

3. The value for a all cases must be of the same data type as the variable in the switch.

Example: The code displays the name of the month, based on the value of month, using the switch statement.

class Simple{
    public static void main(String args[]){

    int month = 4;
    String monthString;

    switch(month){
        case 1:   monthString = "January";
        break;
        case 2:   monthString = "February";
        break;
        case 3:   monthString = "March";
        break;
        case 4:   monthString = "April";
        break;
        case 5:   monthString = "May";
        break;
        case 6:   monthString = "June";
        break;
        case 7:   monthString = "July";
        break;
        case 8:   monthString = "August";
        break;
        case 9:   monthString = "September";
        break;
        case 10:   monthString = "October";
        break;
        case 11:   monthString = "November";
        break;
        case 12:   monthString = "December";
        break;
        default:   monthString = "Invalid month";
        break;
        }
        System.out.println(monthString);
    }
}

Output:

April

Example: The code displays the Decimal to hexaDecimal

class Simple{
    public static void main(String args[]){
    int digitInDecimal = 12;
    switch(digitInDecimal){
        case 0:
        System.out.println("0");
        break;
        case 1:
        System.out.println("1");
        break;
        case 2:
        System.out.println("2");
        break;
        case 3:
        System.out.println("3");
        break;
        case 4:
        System.out.println("4");
        break;
        case 5:
        System.out.println("5");
        break;
        case 6:
        System.out.println("6");
        break;
        case 7:
        System.out.println("7");
        break;
        case 8:
        System.out.println("8");
        break;
        case 9:
        System.out.println("9");
        break;
        case 10:
        System.out.println("A");
        break;
        case 11:
        System.out.println("B");
        break;
        case 12:
        System.out.println("C");
        break;
        case 13:
        System.out.println("D");
        break;
        case 14:
        System.out.println("E");
        break;
        case 15:
        System.out.println("F");
        break;
        default:
        System.out.println("Invalid");
        break;
        }
    }
}

Output:

C

Using Strings in switch Statements

In Java SE 7 and later, you can use a String object in the switch statement's expression. The case statement should be string literal.

Example:

class Simple{
    public static void main(String args[]){
    String dayString = "Sunday";
    int dayNumber;
    switch(dayString){
        case "monday":
            dayNumber = 1;
            break;
        case "Tuesday":
            dayNumber = 2;
            break;
        case "Wednesday":
            dayNumber = 3;
            break;
        case "Thursday":
            dayNumber = 4;
            break;
        case "Friday":
            dayNumber = 5;
            break;
        case "Saturday":
            dayNumber = 6;
            break;
        case "Sunday":
            dayNumber = 7;
            break;
        default:   dayNumber = 0;
            break;
        }
        System.out.println(dayNumber);
    }
}

Output:

7