Java - What is the output: switch String value

Question

What is the output: switch String value

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");
    default:
      System.out.println("in default");
    }
  }
}


Click to view the answer

in case 1
in case 2
in default

Demo

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");
    default://from  w w w . j a v  a2s.  co  m
      System.out.println("in default");
    }
  }
}

Note

You can add break to jump out of case statement

Related Quiz