Which is the Best Java Web Framework ?

Java - Things to know before your OCJP Exam - Switch Statement

1. Only Integers, char, String and Enum are allowed in Switch statement.

    switch(1.1){
     case 1.1: System.out.println("1");
     case 2.1: System.out.println("2");
     case 3.2: System.out.println("3");
    }
    
results in Compilation Error : Cannot switch on a value of type double. Only convertible int values, strings or enum constants are permitted

    switch(1){
      case 1.1: System.out.println("1");
      case 2.1: System.out.println("2");
      case 3.2: System.out.println("3");
    }

results in Compilation Error : Type mismatch: cannot convert from double to int

2. If there are no break statements in case blocks, it will execute all statements either till the next break statement or end of switch block

    switch(1){
        case 1: System.out.println("1");
        case 2: System.out.println("2");
case 3: System.out.println("3"); 
default: System.out.println("default");
    }

results in 1 2 3 default

     switch(1){
case 1: System.out.println("1");
case 2: System.out.println("2");
case 3: System.out.println("3"); 
        break;
          default: System.out.println("default");
     }

results in 1 2 3

3. Default case will be executed if there is no match, and statements thereafter till break or end of switch

       switch(4){
         case 1: System.out.println("1");
         case 2: System.out.println("2");
         default: System.out.println("default");
         case 3: System.out.println("3"); 
       }

results in default 3

4. Two Cases cannot have same value

       switch(1){
           case 1: System.out.println("1");
          break;
            case 1: System.out.println("2");
          break;
           case 3: System.out.println("3");
          break;
            default: System.out.println("default");
          break;
       }

results in Compilation Error: Duplicate case

5. Characters and their ASCII value can be used interchangeably

        switch(65){
           case 'A': System.out.println("1");
           break;
           case 66: System.out.println("2");
           break;
           case 3: System.out.println("3");
           break;
           default: System.out.println("default");
           break;
        }

results in 1.


        switch(65){
           case 'A': System.out.println("1");
           break;
           case 65: System.out.println("2");
           break;
           case 3: System.out.println("3");
           break;
           default: System.out.println("default");
           break;
        }

results in Compilation Error: Duplicate case

6.      In Case of Enum in the switch statements, Enum options with the Enum prefix or default should be the cases.

        enum colour {RED, BLUE, GREEN, BLACK}

        colour cl = colour.BLUE;
switch(cl){
          case BLUE: System.out.println("1");
          break;
          case GREEN: System.out.println("2");
          break;
          case BLACK: System.out.println("3");
          break;
          default: System.out.println("default");
          break;
        } 

results in 1


        enum colour {RED, BLUE, GREEN, BLACK}
        colour cl = colour.BLUE;
switch(cl){
            case BLUE: System.out.println("1");
                            break;
            case GREEN: System.out.println("2");
                              break;
            case YELLOW: System.out.println("3");
                              break;
            default: System.out.println("default");
                              break;
}

results in compilation error: YELLOW cannot be resolved or is not a field

        enum colour {RED, BLUE, GREEN, BLACK}
        colour cl = colour.BLUE;
switch(cl){
            case colour.BLUE: System.out.println("1");
                   break;
            case colour.GREEN: System.out.println("2");
                   break;
            case colour.YELLOW: System.out.println("3");
                   break;
            default: System.out.println("default");
                   break;
        }

results in compilation error: The qualified case label colour.BLUE must be replaced with the unqualified enum constant BLUE.


Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner
comments powered by Disqus




×

Try our new Java Interview Questions Search Tool.

There are more than 1000 questions with practice tests.

It's updated daily and It's Awesome.


×

Try our new Java Practice Test tool.

There are more than 200 questions and expanding quickly. It's updated daily and It's Awesome.