Files
javademos/patterns/preview/PrimitiveSwitch.java
T
Claude bc91b9c8d0 Add patterns module: pattern matching from JDK 21 to 27
Type, record and primitive patterns, guards, sealed exhaustiveness, MatchException via separate compilation, and eight captured transcripts.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TF9JWFvJSNm6HVzswzZU5a
2026-09-24 11:50:40 +00:00

32 lines
996 B
Java

public class PrimitiveSwitch {
static String httpFamily(int status) {
return switch (status) {
case 200 -> "OK";
case int i when i >= 200 && i < 300 -> "other success " + i;
case int i when i >= 400 && i < 500 -> "client error " + i;
case int i -> "something else " + i;
};
}
static String flag(boolean b) {
return switch (b) {
case true -> "yes";
case false -> "no";
};
}
static String big(long n) {
return switch (n) {
case 0L -> "zero";
case 1L -> "one";
case long l -> "many (" + l + ")";
};
}
public static void main(String[] args) {
System.out.println(httpFamily(200) + " | " + httpFamily(204) + " | " + httpFamily(404) + " | " + httpFamily(500));
System.out.println(flag(true) + " " + flag(false));
System.out.println(big(0) + " " + big(1) + " " + big(Long.MAX_VALUE));
}
}