Java gets a new version every six months, with a long-term support (LTS) release every two years: 17, 21 and 25 are the ones most companies run. Recent versions made everyday code much shorter. If you join a project, check its version (java -version) before using these.
var and text blocks
var (Java 10) lets the compiler work out a local variable's type from its value. The type is still fixed and checked; you just don't repeat it. Text blocks (Java 15) write multi-line strings between triple quotes, without \n and escaped quotes; the indentation of the closing quotes sets how much leading space is removed.
Java
var names = new ArrayList<String>();
names.add("Ana");
var json = """
{
"name": "%s",
"tags": ["new"]
}
""".formatted(names.get(0));
System.out.print(json);
Prints
{
"name": "Ana",
"tags": ["new"]
}
Pattern matching for instanceof
Checking a type and then casting used to take two lines. Now instanceof can name the converted value in one go, and the name can be used in the rest of the condition (Java 16):
Java
Object[] things = { "duck", 42, 3.5, List.of(1, 2) };
for (Object o : things) {
if (o instanceof String s && s.length() > 3) System.out.println("long string " + s);
else if (o instanceof Integer n) System.out.println("int, doubled " + n * 2);
else System.out.println("something else: " + o);
}
A switch can now match on types too, with an optional when guard, and handle null (Java 21). It replaces long if instanceof chains:
Java
public class Main {
static String describe(Object o) {
return switch (o) {
case null -> "nothing";
case Integer i when i < 0 -> "a negative number";
case Integer i -> "the number " + i;
case String s -> "text of length " + s.length();
default -> "a " + o.getClass().getSimpleName();
};
}
public static void main(String[] args) {
for (Object o : new Object[] { -4, 7, "hey", null, 2.0 }) System.out.println(describe(o));
}
}
Prints
a negative number
the number 7
text of length 3
nothing
a Double
Sealed types and record patterns
A sealed interface lists every class allowed to implement it (Java 17). Combined with records it models data that is "one of these kinds": a payment is a card, a bank transfer or a voucher, and nothing else. Because the compiler knows the full list, a switch over it needs no default, and adding a new kind later makes every switch that misses it fail to compile, which is exactly where you want to be told. Record patterns (Java 21) take a record apart right in the case:
Java
public class Main {
sealed interface Payment permits Card, Transfer, Voucher {}
record Card(String last4, int cents) implements Payment {}
record Transfer(String iban, int cents) implements Payment {}
record Voucher(String code) implements Payment {}
static int fee(Payment p) {
return switch (p) {
case Card(String last4, int cents) -> cents * 2 / 100;
case Transfer(String iban, int cents) when cents > 100_000 -> 0;
case Transfer t -> 25;
case Voucher v -> 0;
};
}
public static void main(String[] args) {
List<Payment> ps = List.of(new Card("4242", 5000), new Transfer("GB00X", 250_000), new Transfer("GB00Y", 900), new Voucher("FREE"));
int total = 0;
for (Payment p : ps) total += fee(p);
System.out.println(total);
}
}
Prints
125
A card fee is 2% (100 cents on 5000), a big transfer is free, a small one costs 25, and a voucher is free: 125 in all. This style, plain immutable data plus switches over it, is called data-oriented programming. It sits alongside classic objects with methods: use whichever makes the code clearer.
Smaller helpers
List.of, Set.of, Map.of (Java 9): small unmodifiable collections.
String gained strip, isBlank, lines, repeat (11) and formatted (15).
stream.toList() (16) instead of collect(Collectors.toList()).
Math.clamp, List.reversed(), getFirst() and getLast() (21) on lists and other ordered collections.
Helpful NullPointerException messages (14) that name exactly what was null: Cannot invoke "String.length()" because "name" is null.
Don't overuse var: var x = service.load(); hides what x is. It's best when the type is obvious from the right side, like var list = new ArrayList<String>();.
Practice
Is var count = 5; count = "five"; allowed?
More info
Quick reference
var x = ...;
Let the compiler work out a local variable's type (10).