CodesJava

Easy learning with example program codes

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.

Note:

  • 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.codesjava;
 
public class SwitchStringExampleExample {
	public static void main(String args[]){
		String game = "java";  
        switch(game){  
        case "sql":  
            System.out.println("Learn sql");  
            break;  
        case "java":  
            System.out.println("Learn java");  
            break;  
        case "angularjs":  
            System.out.println("Learn angularjs");  
        }
	}
}




Output

Learn java

 

Sign Up/ Sign In
Ask a Question


Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki