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.w3spoint; 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 |