Java autoboxing unboxing in switch statement

Question

What is the output of the following code:

public class Main {
  public static void main(String[] args) {
    Integer iOb = 2;  //from  w  w w .j a va2  s .  c om
    
    switch(iOb) {  
      case 1: System.out.println("one");  
        break;  
      case 2: System.out.println("two");  
        break;  
      default: System.out.println("error");  
    } 
  }
}


two

Note

Because of auto-unboxing, we can use Integer numeric objects to control a switch statement.

When the switch expression is evaluated, iOb is unboxed and its int value is obtained.




PreviousNext

Related