Understanding Modern Language Features

1. Using Switch Expressions

FormDetail
Arrowcase X -> expr;
Block + yieldMulti-statement branch
ExhaustiveRequired when used as expression
No fallthroughEach case independent

2. Using Local Variable Type Inference (var)

AllowedNot Allowed
Local vars with initializerMethod params/return type
For-loop variablesField declarations
Try-with-resourcesWithout initializer
Lambda params Java 11+null initializer alone

3. Understanding var with Lambdas

FormUse
(var x, var y) -> ...Allows annotations on lambda params
Mixed typesForbidden — all params or none
No typevs (x, y) -> ... functionally same

4. Using var in Try-with-Resources

Example: var with resource

try (var in = Files.newBufferedReader(path)) {
    in.lines().forEach(System.out::println);
}
TipReason
Type inferredLess verbose
Use sparinglyLost type clarity hurts readability

5. Using Private Interface Methods (Java 9+)

ModifierUse
privateHelper for default methods
private staticHelper for static methods
VisibilityNot exposed to implementers

6. Using Default Methods in Interfaces

AspectDetail
Syntaxdefault returnType m() { ... }
Diamond resolutionClass > interface; explicit I.super.m()
UseAPI evolution without breaking
CannotOverride Object methods

7. Using Static Methods in Interfaces

RuleDetail
Not inheritedCall via interface name
UseFactory methods (e.g., List.of)
OverrideCannot

8. Using Enhanced Pseudo-Random Generators (Java 17+)

APIUse
RandomGenerator.of("L64X128MixRandom")Modern algorithm
RandomGeneratorFactory.all()List available
SplittableGeneratorParallel streams
JumpableGeneratorSkip-ahead

9. Using Modern String Methods

MethodSinceUse
isBlank()Java 11Empty or whitespace
strip / stripLeading / stripTrailingJava 11Unicode-aware trim
lines()Java 11Stream<String>
repeat(n)Java 11Repeat string
formatted(args)Java 15Instance form
chars/codePointsJava 8IntStream
indent(n)Java 12Add/remove indent

10. Using Collection Factory Methods

FactoryResult
List.of(...)Immutable list
Set.of(...)Immutable set
Map.of(k1,v1,...)≤10 entries
Map.ofEntries(Map.entry(k,v),...)Many entries
List.copyOf(coll)Defensive immutable copy