Java - What is the output: string switch and variable

Question

What is the output of the following code.


public class Main {

  public static void main(String[] args) {

    String switchValue = "1";

    switch (switchValue) {

    case "1":
      System.out.println("in case 1");
    case "2":
      System.out.println("in case 2");
    case switchValue :
      System.out.println("in case switchValue"); 

    default:
      System.out.println("in default");
    }
  }
}


Click to view the answer

case expressions must be constant expressions

Note

You cannot use variable in the switch case.

Related Quiz