Review of important Java changes from version 8 till 21
Tags: beginners
Published: 2023-07-06 19:27:34

## Java 8

*Lambda Expressions** — The introduction of lambda expressions is the most important change in Java 8. They enable the creation of anonymous functions and promote a functional programming style.

```
List<String> names = Arrays.asList(“Jan”, “Adam”, “Ewa”);
Collections.sort(names, (a, b) -> b.compareTo(a));
```

*Stream APIs** — A new API is used to support streaming operations on data in collections, fundamentally changing the way data is manipulated.

```
List<String> names = Arrays.asList(“Jan”, “Adam”, “Ewa”);
List<String> upperCaseNames = names.stream()
.map(String::toUpperCase)
.collect(Collectors.toList());
```

**Interfaces with default methods** — Java 8 introduced the ability to define default methods in interfaces, allowing new functions to be added to existing interfaces without compromising compatibility.

### Java 9

**Jigsaw (Project Jigsaw)** — The introduction of modularity to the Java platform is a major feature of Java 9. Modules make it easier to scale applications for both small and large projects.

**REPL (JShell)** — JShell is an interactive command shell that allows you to quickly test code snippets without having to create a full class.

### Java 10

**Local type inference** — Java 10 introduces type inference for local variables, which means we no longer have to declare the type of the variable — the compiler will do it itself.

```
var text = “Hello, Java 10”; // Type Inference
System.out.println(text);
```

### Java 11

**Running .java files** — Since Java 11, it is possible to directly run .java files without first compiling them to .class.

**HTTP Client API** — The new HTTP client API, which was available as an “incubator feature” in Java 9 and 10, is now a full-fledged API as of Java 11.

### Java 12

*Switch Expressions** — The switch has been improved and now offers a more concise and secure syntax.

```
String day = “MONDAY”;
String result = switch (day) {
case “MONDAY”, “TUESDAY”, “WEDNESDAY”, “THURSDAY”, “FRIDAY” -> “Weekday”;
case “SATURDAY”, “SUNDAY” -> “Weekend”;
default -> throw new IllegalArgumentException(“Invalid day: “ + day);
};
```

### Java 13

**Text Blocks** — The introduction of text blocks has made it easier to work with multi-line text in Java.

### Java 14

**Records** — Records are a new type of references in Java, which are non-mutable data. They make it easier to create classes that are just data carriers.

public record Point(int x, int y) {} // Defining a record

**Pattern Matching for instanceof** — This feature increases code readability by eliminating unnecessary type casting.

```
Object obj = “Hello”;
if (obj instanceof String s) {
System.out.println(s.toLowerCase());
}
```

### Java 15

**Sealed Classes** — A sealed class allows programmers to specify which other classes can inherit from it.

```
public sealed class Shape
permits Circle, Rectangle {}

public final class Circle extends Shape {…}

public final class Rectangle extends Shape {…}
```

### Java 16

**Records (stable version)** -Records that were previously available

available in the preview version, have become full-fledged language elements in Java 16.

**Pattern Matching for instanceof (Stable Version)** — As with records, the pattern feature for instanceof, which was previously available in the preview version, has been officially introduced in Java 16.

### Java 17

**Sealed Classes (Stable Version)** — In Java 17, the sealed class, which was previously available as a preview feature, has become a full-fledged part of the language.

**Pattern Matching for Switch (Preview)** — Java 17 introduced patterns for switch expressions as a preview feature, which has the potential to increase code readability and security.

### Java 18

**Project Panama (Preview)** — This is one of the most anticipated projects for Java. The goal of Panama is to improve Java’s interoperability with native C/C++ code.

### Java 19

**Project Valhalla (Preview)** — This is a long-awaited project that aims to introduce value types (value types) and specialization of generics into Java, improving performance and memory.

### Java 20

**Project Loom (Preview)** — Project Loom aims to add lightweight threads (so-called “fibres”) to the JVM, increasing the efficiency of multithreading in Java.

### Java 21 — upcoming

Although plans for Java 21 are still unclear, we can expect further development and fixes for preview features from previous versions, as well as new features that will continue the development of Java as a programming language and platform.

This is truly an exciting time for Java developers, as the language continues to evolve with new features and enhancements designed to make developers’ daily work easier. We look forward to the next versions of Java!