Switch Case || Java example

Java switch case

When we have multiple options and based on condition we need to choose one then we can use switch condition.

For example when we have 4 options and based on user input we need print a message this is the best scenario to use java switch case condition.

We might also use if else condition but it requires providing multiple condition and time taken is also more compared to switch case.

 

If Condition :

if (i == 1) {

    System.out.println(" One  ");
} else if (i == 2) {

    System.out.println(" Two ");
} else if (i == 3) {

    System.out.println(" Three ");
} else if (i == 4) {

    System.out.println(" Four ");
}

 

We can see multiple if conditions here, it takes time to test every condition

 

But we can handle this in a simple way usingĀ  java switch case

 

Switch Condition:

Here we provide a integer to variable i then it will check for which case it belongs to and perform them here there are no multiple conditions.

 

We can use char, byte, short, int, String, enum with switch conditions and cannot use double, float.

 

Note:

When the input doesn’t match with any case we have a default case

default:
     ...
     break;

 

switch (i) {

    case 1:
        ...
        break;

    case 2:
        ...
        break;

    case 3:
        ...
        break;

    case 4:
        ...
        break;
   
    default:
        ...
        break;
}

 

Using integer values

public class Switch {

    public static void main(String args[]) {

        int i = 1;

        switch (i) {

            case 1:
                System.out.println(" One ");
                break;

            case 2:
                System.out.println(" Two ");
                break;

            case 3:
                System.out.println(" Three ");
                break;

            case 4:
                System.out.println(" Four ");
                break;

            default:
                System.out.println(" No Input Found ");
                break;

        }
    }
}

 

java switch case

 

Using String’s

We can use even String in case checking.

public class Switch {

    public static void main(String args[]) {

        String str = "af";

        switch (str) {

            case "a":
                System.out.println(" One ");
                break;

            case "b":
                System.out.println(" Two ");
                break;

            case "c":
                System.out.println(" Three ");
                break;

            case "d":
                System.out.println(" Four ");
                break;

            default:
                System.out.println(" No Input found ");
                break;
        }
    }
}

 

java switch case

 

 

Show Buttons
Hide Buttons
Read previous post:
Android Room Database || CRUD || Part 2

  Room database For part 1 of room database tutorial visit and for video tutorial visit https://androidcoding.in/2020/05/01/android-room-database-crud-part-1/   For more...

Close