1. assert
Marks a runtime check that throws AssertionError when the expression is false.
assert value > 0 : "value must be positive";
Enable with -ea on the JVM.
2. default
Two contexts:
- Interface method – supplies a concrete implementation since Java 8.
- Annnotation element – supplies a fallback value.
interface Logger {
default void info(String msg) {
System.out.println("[INFO] " + msg);
}
}
@interface Retryable {
int maxAttempts() default 3;
}
3. transient
Excludes a field from Java’s built-in serialization mechanism.
class User implements Serializable {
private String login;
private transient String password; // never serialized
}
4. strictfp
Guarantees identical floating-point results across JVMs by enforcing IEEE 754 rules. Redundant from Java 17 onward.
strictfp class Calc {
strictfp double sum(double a, double b) { return a + b; }
}
5. volatile
Ensures visibility and prevents instruction re-ordering for variables accessed by multiple threads.
class TaskRunner {
private volatile boolean running = true;
void stop() { running = false; }
void loop() {
while (running) {
// work
}
}
}
6. native
Declares a method implemented in platform-specific code (JNI).
public class Display {
public native void show(); // implemented in C/C++
static { System.loadLibrary("display"); }
}
7. Module directives
Introduced in Java 9.
module– starts a module descriptor.exports– exposes a package.requires– declares a dependency.opens– grants reflective access.provides … with– registers service implementations.
module com.example.app {
requires java.net.http;
exports com.example.api;
opens com.example.internal to junit;
}
8. yield
Returns a value from a switch expression (Java 14+).
int days = switch (month) {
case JAN, MAR, MAY -> 31;
case APR, JUN, SEP -> 30;
case FEB -> {
System.out.println("Leap year check");
yield 28;
}
default -> throw new IllegalArgumentException();
};
9. sealed and permits
Restrict wich classes may extend or implement a type (Java 15+).
public sealed class Shape permits Circle, Rectangle { }
final class Circle extends Shape { }
final class Rectangle extends Shape { }
10. var
Local-variable type inference (Java 10+). Must be initialized and cannot denote null.
var names = new ArrayList<String>();
for (var n : names) System.out.println(n);
11. to and when
Reserved for future language features; no semantic role in Java 23 yet.