Feature Name | Description |
---|---|
New Licence | Java 17 is issued under the new NFTC (Oracle No-Fee Terms and Conditions) license. It is therefore again allowed to use the Oracle JDK version for free for production and commercial use. |
Text Blocks Preview in Java 13 |
Triple quotes """ text blocks allowedprivate static void jsonBlock() { String text = """ { "name": "John Doe", "age": 45, "address": "Doe Street, 23, Java Town" } """; // the indent of ending could determine the preceding spaces for block System.out.println(text); } |
Switch Expressions Simplier format, more compact no break; needed |
private static void withSwitchExpression(Fruit fruit) { switch (fruit) { // could be assigned to var like String res = switch ... case APPLE, PEAR -> System.out.println("Common fruit"); case ORANGE, AVOCADO -> System.out.println("Exotic fruit"); default -> System.out.println("Undefined fruit"); } } |
Records Records will allow you to create simple immutable data classes. Preview in Java 14 |
public record GrapeRecord(Color color, int nbrOfPits) { // no need to define getters etc. Can override constructor. } // Also possible to define anywhere in the code record GrapeRecord(Color color, int nbrOfPits) {} GrapeRecord grape1 = new GrapeRecord(Color.BLUE, 1); |
Sealed Classes more a feature useful for library owners. Let you allow who can extend abstract class and who cannot Preview in Java 15 |
public abstract sealed class FruitSealed permits AppleSealed, PearSealed { } public non-sealed class AppleSealed extends FruitSealed { } public final class PearSealed extends FruitSealed { } |
Pattern matching for instanceof Preview in Java 14 |
if (o instanceof GrapeClass grape) { // now can auto assign grape to o with casting to class System.out.println("This grape has " + grape.getNbrOfPits() + " pits."); } |
Helpful NullPointerExceptions In Java 14 |
With Java 17, the same code results in the following output which shows exactly where the NullPointerException occured at which class/instance. |
Compact Number Formatting Support A factory method is added to NumberFormat in order to format numbers in compact, human-readable form |
NumberFormat fmt = NumberFormat.getCompactNumberInstance(Locale.ENGLISH, NumberFormat.Style.SHORT); Outputs like: 1K, 100K, 1M or 1 thousand, 100 thousand, 1 million |
Day Period Support Added A new pattern B is added for formatting a DateTime which indicates a day period according to the Unicode standard. |
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("B"); System.out.println(dtf.format(LocalTime.of(8, 0)));Outputs like: in the morning, in the afternoon, in the evening, at night, midnight |
Stream.toList() In order to convert a Stream to a List, you need to call the collect method with Collectors.toList(). In Java 17, a toList method is added which replaces the old behaviour. |
Stream |