Java – Switch Case statements with String

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

1. 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

2. 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,

  1. Switch Case With int data type
  2. Switch Case With char data type
  3. Switch Case With String data type
  4. Switch Case with String data type for null input

2.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

2.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

2.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

2.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 !!

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java - Declaration, Definition, Initialization and Instantiation, Instance of class