Java: Switch Case statement with Strings

In this article, we will learn and demonstrate simple example based on Switch case statement with different data type like int, char and then finally String

 

Till Java 6,

  • Switch case statement can iterate over either int or char data type
  • No feature to iterate over String data type
  • This is the shortcoming and Java developers are forced to use if-else-if conditional statement impacting performance at times

 

Post Java 7 release,

  • With the release of Java version 7, a new feature to iterate over String data type has been added
  • Much to the relief of Java developer who are forced to use alternate if-else-if conditional statement
  • Switch case statement with String data type has a good performance over multiple if-else-if chained condition
  • String data type compares the string passed with equals() method from String class
  • As you can see, it uses equals() method from String class which clearly indicates that the comparison is CASE-Sensitive
  • Handle null case properly otherwise will end up with NullPointerException

 

Let us see an example for each one,

  • Switch Case With int data type
  • Switch Case With char data type
  • Switch Case With String data type

 

Program 1: Switch Case with int data type

SwitchCaseWithInt.java

package in.bench.resources.programs;

public class SwitchCaseWithInt {

	public static void main(String[] args) {
		String day = checkDayOfWeekBasedOnNumber(5);
		System.out.println(day);
	}

	private static String checkDayOfWeekBasedOnNumber(int inputDayNumber){

		String dayOfWeek = null;

		switch (inputDayNumber) {

		case 1:  
			dayOfWeek = "Monday";
			break;
		case 2:  
			dayOfWeek = "Tuesday";
			break;
		case 3:  
			dayOfWeek = "Wednesday";
			break;
		case 4:  
			dayOfWeek = "Thursday";
			break;
		case 5:  
			dayOfWeek = "Friday";
			break;
		case 6:  
			dayOfWeek = "Saturday";
			break;
		case 7:  
			dayOfWeek = "Sunday";
			break;
		default:
			dayOfWeek = inputDayNumber + " is invalid input day no.";
		}
		return dayOfWeek;
	}
}

Output:

Friday

 

Program 2: Switch Case with char data type

SwitchCaseWithChar.java

package in.bench.resources.programs;

public class SwitchCaseWithChar {

	public static void main(String[] args) {
		String fruit = checkFruitBasedOnChar('d');
		System.out.println(fruit);
	}

	private static String checkFruitBasedOnChar(char inputFruitChar) {

		String fruitName = null;

		switch (inputFruitChar) {

		case 'a':  
			fruitName = "A for Apple";
			break;
		case 'b':  
			fruitName = "B for Banana";
			break;
		case 'c':  
			fruitName = "C for Cherry";
			break;
		case 'd':  
			fruitName = "D for Date Plum";
			break;
		default:
			fruitName = inputFruitChar + " is invalid input fruit char";
		}
		return fruitName;
	}
}

Output:

D for Date Plum

 

Program 3: Switch Case with String data type

SwitchCaseWithString.java

package in.bench.resources.programs;

public class SwitchCaseWithString {

	public static void main(String[] args) {
		String returnQuarterResult = checkQuarterBasedOnMonth("April");
		System.out.println(returnQuarterResult);
	}

	private static String checkQuarterBasedOnMonth(String inputMonthStr){

		String quarter = null;

		switch (inputMonthStr) {

		case "January":  
			quarter = "It is start of LAST quarter acc. to some region";
			break;
		case "April":  
			quarter = "It is start of FIRST quarter acc. to some region";
			break;
		case "July":  
			quarter = "It is start of SECOND quarter acc. to some region";
			break;
		case "October":  
			quarter = "It is start of THIRD quarter acc. to some region";
			break;
		default:
			quarter = "It's NOT start of any quarter acc. to some region";
		}
		return quarter;
	}
}

Output:

It is start of FIRST quarter according to some region

 

Program 4: Switch Case with String data type for null input

SwitchCaseWithString.java

package in.bench.resources.programs;

public class SwitchCaseWithString {

	public static void main(String[] args) {
		String returnQuarterResult = checkQuarterBasedOnMonth(null);
		System.out.println(returnQuarterResult);
	}

	private static String checkQuarterBasedOnMonth(String inputMonthStr){

		String quarter = null;

		switch (inputMonthStr) {

		case "January":  
			quarter = "It is start of LAST quarter acc. to some region";
			break;
		case "April":  
			quarter = "It is start of FIRST quarter acc. to some region";
			break;
		case "July":  
			quarter = "It is start of SECOND quarter acc. to some region";
			break;
		case "October":  
			quarter = "It is start of THIRD quarter acc. to some region";
			break;
		default:
			quarter = "It's NOT start of any quarter acc. to some region";
		}
		return quarter;
	}
}

Output:

Exception in thread "main" java.lang.NullPointerException
	at in.bench.resources.programs.SwitchCaseWithString
              .checkQuarterBasedOnMonth(SwitchCaseWithString.java:14)
	at in.bench.resources.programs.SwitchCaseWithString
                                   .main(SwitchCaseWithString.java:6)

 

Solution: Perform null check with if condition to avoid NullPointerException

 

 

That’s all folks !!

 

References:

https://docs.oracle.com/javase/8/docs/technotes/guides/language/strings-switch.html
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

 

Read Also:

 

Happy Coding !!
Happy Learning !!

Declaration, Definition, Initialization and Instantiation, Instance of class