Java 7 String in Switch Case Statement

Java switch statement:

Java switch statement is used to execute a block of statement based on the switch expression value. It is like if else if statement.

Java 7 allows us to use string objects in the expression of switch statement.


  • Java switch case String is case sensitive.
  • Java switch case uses String.equals() method to compare the expression value with case values, so it will throw NullPointerException if passed value is null.

Java string in switch case example

package com.w3schools360;

public class SwitchStringExampleExample {
	public static void main(String args[]){
		String game = "java";  
        case "sql":  
            System.out.println("Learn sql");  
        case "java":  
            System.out.println("Learn java");  
        case "angularjs":  
            System.out.println("Learn angularjs");  


Learn java