java7新特性之—String values in a switch statement
Strings in switch
The Java switch statement allows you to write an efficient multiple-branch statement
without lots and lots of ugly nested ifs—like this:
without lots and lots of ugly nested ifs—like this:
public void printDay(int dayOfWeek) {
switch (dayOfWeek) {
case 0: System.out.println("Sunday"); break;
case 1: System.out.println("Monday"); break;
case 2: System.out.println("Tuesday"); break;
case 3: System.out.println("Wednesday"); break;
case 4: System.out.println("Thursday"); break;
case 5: System.out.println("Friday"); break;
case 6: System.out.println("Saturday"); break;
default: System.err.println("Error!"); break;
}
}
In Java 6 and before, the values for the cases could only be constants of type byte,
char, short, int (or, technically, their reference-type equivalents Byte, Character,
Short, Integer) or enum constants. With Java 7, the spec has been extended to allow
for the String type to be used as well. They’re constants after all.
public void printDay(String dayOfWeek) {
switch (dayOfWeek) {
case "Sunday": System.out.println("Dimanche"); break;
case "Monday": System.out.println("Lundi"); break;
case "Tuesday": System.out.println("Mardi"); break;
case "Wednesday": System.out.println("Mercredi"); break;
case "Thursday": System.out.println("Jeudi"); break;
case "Friday": System.out.println("Vendredi"); break;
case "Saturday": System.out.println("Samedi"); break;
default: System.out.println("Error: '"+ dayOfWeek
➥ +"' is not a day of the week"); break;
}
}
In all other respects, theswitch statement remains the same. Like many Project Coin
enhancements, this is really a very simple change to make life in Java 7 a little bit easier.
该新特性只是java的语法糖。
Syntactic sugar
A phrase that’s sometimes used to describe a language feature is “syntactic sugar.”
This means that the syntax form is redundant—it already exists in the language—but
the syntactic sugar form is provided because it’s easier for humans to work with.
As a rule of thumb, a feature referred to as syntactic sugar is removed from the compiler’s representation of the program early on in the compilation process—it’s said
to have been “desugared” into the basic representation of the same feature.
This makes syntactic sugar changes to a language easier to implement because they
usually involve a relatively small amount of work, and only involve changes to the
compiler (javac in the case of Java).
读书笔记:The Well-Grounded Java Developer
A phrase that’s sometimes used to describe a language feature is “syntactic sugar.”
This means that the syntax form is redundant—it already exists in the language—but
the syntactic sugar form is provided because it’s easier for humans to work with.
As a rule of thumb, a feature referred to as syntactic sugar is removed from the compiler’s representation of the program early on in the compilation process—it’s said
to have been “desugared” into the basic representation of the same feature.
This makes syntactic sugar changes to a language easier to implement because they
usually involve a relatively small amount of work, and only involve changes to the
compiler (javac in the case of Java).
读书笔记:The Well-Grounded Java Developer