Java 27 Finally Lets You Switch on long, double, boolean — With Range Guards
Java 27's preview feature extends switch expressions to all primitive types (long, float, double, boolean) and adds pattern matching with 'when' guards, enabling concise range-based business rules instead of if-else chains, though it remains a preview feature requiring explicit enablement.
Java 27 introduces Primitive Types in Patterns, instanceof, and switch as a preview feature (fifth preview), allowing switch expressions to work directly with all primitive types: long, float, double, and boolean. Previously, switch only accepted byte, short, char, int, String, and enum.
Basic primitive switch
Simple equality matching now works for primitives:
long value = 100L;
switch (value) {
case 100L -> System.out.println("100");
case 200L -> System.out.println("200");
default -> System.out.println("other");
}Guarded patterns for range checks
The more powerful addition is pattern matching with when guards , letting you express range conditions naturally:
case long amount when amount < 10_000L -> RiskLevel.LOW;
case long amount when amount < 100_000L -> RiskLevel.MEDIUM;
case long amount when amount < 1_000_000L -> RiskLevel.HIGH;
default -> RiskLevel.VERY_HIGH;Each case long amount binds the selector to a variable, and when adds a boolean guard. This mirrors Oracle's own documentation examples.
Real-world refactoring examples
Order risk calculation (long amount in cents)
Before: chained if/else if with multiple thresholds. After: a switch expression that reads like a rule table:
public RiskLevel calculateRisk(long amountFen) {
return switch (amountFen) {
case long amount when amount < 0 ->
throw new IllegalArgumentException("amount must not be negative");
case long amount when amount < 10_000L -> RiskLevel.LOW;
case long amount when amount < 100_000L -> RiskLevel.MEDIUM;
case long amount when amount < 1_000_000L -> RiskLevel.HIGH;
default -> RiskLevel.VERY_HIGH;
};
}Membership tiers (long points)
public String level(long points) {
return switch (points) {
case long p when p < 1_000L -> "NORMAL";
case long p when p < 10_000L -> "SILVER";
case long p when p < 100_000L -> "GOLD";
default -> "VIP";
};
}File upload strategy (long fileSize in bytes)
@Service
public class UploadStrategyService {
public UploadMode resolve(long fileSize) {
return switch (fileSize) {
case long size when size < 0 ->
throw new IllegalArgumentException("invalid file size");
case long size when size <= 10 * 1024 * 1024L -> UploadMode.DIRECT;
case long size when size <= 500 * 1024 * 1024L -> UploadMode.MULTIPART;
default -> UploadMode.LARGE_FILE;
};
}
}Boolean switch (exhaustive without default)
Since boolean has only two values, a switch covering true and false needs no default:
String result = switch (success) {
case true -> "SUCCESS";
case false -> "FAILED";
};Double-based rating (with precision warning)
double score = 4.6;
String rating = switch (score) {
case double s when s < 0D -> "INVALID";
case double s when s < 3D -> "BAD";
case double s when s < 4D -> "NORMAL";
case double s when s <= 5D -> "GOOD";
default -> "INVALID";
};Important: The author emphasizes that switch supporting double does not mean you should use double for monetary values. Precision concerns (use BigDecimal or long cents) remain separate from language expressiveness.
Broader context: unifying primitives and reference types
This change extends pattern matching ( instanceof) to primitives, part of a longer-term direction (Valhalla, value objects, primitive classes) to avoid treating primitives and reference types as "two completely different worlds" in the type system.
Preview status and enablement
The feature is still preview (JEP 455). To use it:
Compile: javac --release 27 --enable-preview Main.java Run: java --enable-preview Main Maven: configure maven-compiler-plugin with <release>27</release> and <compilerArgs>--enable-preview</compilerArgs>; also add --enable-preview to maven-surefire-plugin for tests.
Practical advice
Do not rush to rewrite production if/else chains just because the syntax is nicer. Preview features can change.
Java 27 is a non-LTS release; wait for the feature to become standard (likely in a future LTS) before large-scale adoption.
Experiment locally with demos to understand how the feature shifts code from "control flow" to "rule description."
The author concludes that while the feature arrived late, it genuinely improves how business rules can be expressed in Java.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
LuTiao Programming
LuTiao Programming is a friendly community offering free programming lessons. We inspire learners to explore new ideas and technologies and quickly acquire job-ready skills.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
