StackTips

Switch-Case statement in Java

nilan avtar

Written by

Nilanchala,  5 min read,  3.16K views, updated on Sept. 17, 2023

Switch case is another alternative for if-else statements. Switch case statement compares value in the object in the expression with the expressions associated with each case. A switch works only with primitive types like byte, short, char, and int and enumerated data types. But since Java SDK7, it also supports String type.

Switch-Case statement in Java

The following code example demonstrates switch case with example. An integer value day is passed as an parameter and the code will display the day of week.

public class StringSwitch {

	public static void main(String[] args) {
		int day = 2;
		String dayOFWeek = "";
		switch (day) {
		case 1:
			dayOFWeek = "MON";
			break;
		case 2:
			dayOFWeek = "TUE";
			break;
		case 3:
			dayOFWeek = "WED";
			break;
		case 4:
			dayOFWeek = "THU";
			break;
		case 5:
			dayOFWeek = "FRI";
			break;
		case 6:
			dayOFWeek = "SAT";
			break;
		case 7:
			dayOFWeek = "SUN";
		default:
		}
		System.out.println(dayOFWeek);
	}
}
  • The older version of java supports switch case statement only with primitive types. It doesn’t supports String comparisons using switch case.
  • But since JDK 7 release, you can use a String object in the expression of a switch statement.
  • The switch statement compares the String object in its expression with the expressions associated with each case label as if it were using the String.equals method; consequently, the comparison of String objects in switch statements is case sensitive.
  • Java compiler generates generally more efficient byte code from switch statements that use String objects than from chained if-then-else statements.

The following code example demonstrates switch case with using String comparison example. Note that, you Java to compile with Java 7 and above.

public class StringSwitch {
	public static void main(String[] args) {
		String key = "TWO";
		switch (key) {
		case "ONE":
			key = "One Selected!";
			break;
		case "TWO":
			key = "Two Selected!";
			break;
		case "THREE":
			key = "Three Selected!";
			break;
		case "FOUR":
			key = "Four Selected!";
			break;
		case "FIVE":
			key = "Five Selected!";
			break;
		case "SIX":
			key = "Six Selected!";
			break;
		default:

		}
		System.out.println(key);

	}
}

Beginner's Guide to Java Collections

This course covers the fundamentals of java collections framework covering different data structures in Java to store, group, and retrieve objects.

>> CHECK OUT THE COURSE
nilan avtar

Nilanchala

I'm a blogger, educator and a full stack developer. Mainly focused on Java, Spring and Micro-service architecture. I love to learn, code, make and break things.

Related posts

Let’s be friends!

🙌 Stay connected with us on social media for the latest updates, exclusive content, and more. Follow us now and be part of the conversation!