What will the following code print when run?
public class Main { public void switchString (String input){ switch (input){ case "a" : System.out.println ( "apple" ); case "b" : System.out.println ( "bat" ); break; case "B" : System.out.println ( "big bat" ); default : System.out.println ( "none" ); }// www. j a v a2 s. c o m } public static void main (String [] args) throws Exception { Main tc = new Main (); tc.switchString ("B"); } }
Select 1 option
A. bat big bat B. big bat none C. big bat D. bat E. The code will not compile.
Correct Option is : B
As of JDK 7 release, you can use a String object in the expression of a switch statement.