Mentioned switch
, should be familiar to all Java developers. switch
Different logic is executed according to the value of the variable, which is used to replace multi-branch if/else
.
However, switch
there are some common problems with using statements.
The first is the execution of the branch switch
in the statement , which is carried out downward by default, and needs to be added to jump out of the execution. This behavior is prone to bugs.case
break
switch
trafficControl
For example, the method in the following code RED
will execute all case
branches when the parameter value is , because no break
statement is added.
public class SwitchStatement {
enum TrafficColor {
RED, YELLOW, GREEN
}
public void trafficControl(TrafficColor color) {
switch (color) {
case RED:
this.stop();
case GREEN:
this.go();
default:
this.watchOut();
}
}
private void go() {
System.out.println("Go!");
}
private void stop() {
System.out.println("Stop!");
}
private void watchOut() {
System.out.println("Be careful!");
}
}
switch
Another common use of statements is to assign values to the same variable in different branches. As shown in the code below. This way of writing is more cumbersome.
public class SwitchStatementAssignment {
enum TrafficColor {
RED, YELLOW, GREEN
}
public String getMessage(TrafficColor color) {
String message;
switch (color) {
case RED:
message = "Stop!";
break;
case GREEN:
message = "Go!";
break;
default:
message = "Be careful!";
}
return message;
}
}
To solve these two problems, switch
expressions and arrow labels can be used.
switch
Expressions were introduced as a preview feature in Java 12, previewed again in Java 13, and made official in Java 14.
switch
Can be used as an expression and thus can have its own value. This simplifies the assignment operation. switch
The value of the expression is determined by the branch. The following code shows switch
the basic usage of expressions.
public class SwitchExpression {
public String formatGifts(int number) {
return switch (number) {
case 0 -> "no gifts";
case 1 -> "only one gift";
default -> number + " gifts";
};
}
}
In the above code, switch
the expressions use arrows instead of traditional colons. After using the arrow, code execution does not go to the next branch, which is equivalent to adding break
. In the code below, the same trafficControl
method, after using the arrow label, is no longer needed break
.
public class SwitchLabel {
enum TrafficColor {
RED, YELLOW, GREEN
}
public void trafficControl(TrafficColor color) {
switch (color) {
case RED -> System.out.println("Stop!");
case YELLOW -> System.out.println("Be careful!");
case GREEN -> System.out.println("Go!");
}
}
}
In most cases, a single expression after the arrow label will suffice. If you have complex logic, you can use code blocks. At this time, it needs yield
to be used to provide a value.
public class YieldValue {
public String formatGifts(int number) {
return switch (number) {
case 0 -> "no gifts";
case 1 -> "only one gift";
default -> {
if (number < 0) {
yield "no gifts";
} else {
yield number + " gifts";
}
}
};
}
}
switch
It can also be used in statements using traditional labels yield
.
public class YieldValue2 {
public String formatGifts(int number) {
return switch (number) {
case 0:
yield "no gifts";
case 1:
yield "only one gift";
default: {
if (number < 0) {
yield "no gifts";
} else {
yield number + " gifts";
}
}
};
}
}
The content of the expression switch
is introduced here. See my B station for the corresponding video.
Push my new book "Quarkus Cloud Native Microservice Development Practical Combat", just search for the title on Jingdong Taobao.