Control flow
You could say that control flow is the bread and butter of program writing. We will begin by exploring two conditional expressions: if
and when
.
The if expression
In Java, the if
statement is not an expression and does not return a value. Let’s examine the following function, which returns one of two possible values:
public String getUnixSocketPolling(boolean isBsd) {
if (isBsd) {
return "kqueue";
} else {
return "epoll";
}
}
While this example is easy to understand, it is generally discouraged to have multiple return
statements, as it can make code more difficult to comprehend.
We can rewrite this method using Java’s var
keyword:
public String getUnixSocketPolling(boolean isBsd) {
var pollingType = "epoll";
if (isBsd) {
pollingType = "kqueue";
}
return pollingType;
}
In our previous example, we used a single return
statement but had...