示例:在字符串上实现switch语句
// Java Program to implement String on switch statements in Java
class Main {
public static void main(String[] args) {
// create a string
String language = "Java";
switch(language) {
case "Java":
System.out.println(language + " is famous for enterprise applications.");
break;
case "JavaScript":
System.out.println(language + " is famous for frontend and backend.");
break;
case "Python":
System.out.println(language + " is famous for ML and AI.");
break;
default:
System.out.println(language + " not found on record.");
break;
}
}
}
输出
Java is famous for enterprise applications.
在上面的示例中,我们在字符串上实现了switch语句。 Java 7中引入了此功能。