
https://horstmann.com/presentations/2024/jcrete
Slide navigation: Forward with space bar, →, or swipe left. Backwards with ← or swipe right. Switch slides with PgDn and PgUp.
switch statement invented in C
switch (c) {
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
ndigit[c-'0']++;
break;
case ' ':
case '\n':
case '\t':
nwhite++;
break;
default:
nother++;
break;
}
if/else if/else if with ≥ 10 casesswitch, average of 7 cases, 3% with fallthroughpublic int length(LispyList lst) {
return switch (lst) {
case EmptyList _ -> 0;
case NonEmptyList(_, tail) -> 1 + length(tail);
}
}
Consider these case clauses:
case 5 -> "Friday"; // 1
case 5 -> "Friday".toUpperCase(); // 2
case 5 -> yield "Friday"; // 3
case 5 -> { break; } // 4
case 5 -> if (Math.random() < 0.5) System.out.println("Friday"); // 5
case 5 -> throw new Exception("Friday"); // 6
How many of these can be valid in a switch statement?
Which of the following are legal switch expressions?
Object x = ...;
String result = switch (x) { // I
case "" -> "empty";
case 0 -> "zero";
default -> "something else";
};
enum Size { SMALL, MEDIUM, LARGE, EXTRA_LARGE };
Object y = ...;
result = switch (y) { // II
case Size.EXTRA_LARGE -> "extra large";
default -> "something else";
};
How many of these switch expressions throw a NullPointerException?
record Box<T>(T contents) { }
Box<String> boxed = null;
String unboxed = switch (boxed) {
case Box(String s) -> s;
};
boxed = new Box<>(null);
String unboxed = switch (boxed) {
case Box(String s) -> s;
};
Box<Box<String>> doubleBoxed = new Box<>(null);
String unboxed = switch (doubleBoxed) {
case Box(Box(String s)) -> s;
};
Which of these switch expressions compile?
Object x = ...;
String d = switch (x) { // I
case Number n -> "a number";
case Integer i -> "an integer";
default -> "something else";
};
Integer y = ...;
String e = switch (y) { // II
case Integer i when i > 0 -> "positive";
default -> "negative";
case 0 -> "zero";
};
void main() {
int count = 0;
float sum = 0;
for (int i = 1; i <= 100; i++) {
if (1.0 / i instanceof float f) {
count++;
sum += f;
}
}
System.out.println(count + " " + sum);
}
What does the program print?
Integer foo(int x) {
int y = switch (x) {
case 0 -> throw new NullPointerException();
case 1 -> throw new IllegalArgumentException();
default -> throw new IndexOutOfBoundsException();
};
return y;
}
int main() {
System.out.println(
switch (foo(3)) {
case 0 -> "zero";
case null -> "null";
default -> "something else";
case throws RuntimeException -> "oh noes";
});
}
What will happen?
foo fails to compilemain fails to compileSuppose the j.u.r.Pattern class is enhanced with this deconstructor:
public inverse String match(String... groups) {
Matcher m = matcher(that); // *that* is the match candidate
if (m.matches()) // receiver for matcher() is the Pattern
match(IntStream.range(1, m.groupCount())
.map(m::group)
.toArray(String[]::new));
}
What is the result of
switch("0:59") {
case Pattern.of("([0-2][0-9]):([0-9]+)").match(h, m) -> 60 * Integer.parseInt(h) + Integer.parseInt(m);
default -> -1;
}
MatchError is throwngroups is not used
switch and instanceof for pattern matching leverages existing knowledgenulldefault