15.Q. Can String be used in the switch case in Java?

Category: JAVA STRING INTERVIEW QUESTIONS AND ANSWERS

Answer:

Yes , String can be used in the switch case in Java.

Example:

public class StringSwitchCase{

public static void main(String args[] ) {

String gift =”Apple Watch”;

switch(gift ) {

case “IPhone” :

System.out.println(“gift is Iphone”);

break;

case “Car” :

System.out.println(“Gift is car”);

break;

case “AppleWatch” :

System.out.println(“Gift is apple watch”);

break;

default :

System.out.println(“You are winning some gifts”);

}

}

OUTPUT:

Gift is apple watch

Leave a Reply

Your email address will not be published. Required fields are marked *