Better Programming

Advice for programmers.

Follow publication

6 Java 17 Features I Didn’t Know About

Konrad
Better Programming
Published in
6 min readJan 8, 2022

--

Photo by Christopher Gower on Unsplash

Local Variable Type Inference aka var

List<String> names = List.of(“Bob”, “Jack”, “Meg”);
List<String> names = List.of(“Bob”, “Jack”, “Meg”);Map<String, Long> namesFrequency = names.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
List<String> names = List.of(“Bob”, “Jack”, “Meg”);Collector<String, ?, Map<String, Long>> byOccurrence = Collectors.groupingBy(Function.identity(), Collectors.counting()Map<String, Long> namesFrequency = names.stream().collect(byOccurrence);
var names = List.of(“Bob”, “Jack”, “Meg”);var byOccurrence = groupingBy(identity(), counting());var namesFrequency = names.stream().collect(byOccurrence);
var names = List.of(“Bob”, “Jack”, “Meg”);var byOccurrence = groupingBy(Function.<String>identity(), counting());var namesFrequency = names.stream().collect(byOccurrence);
Example how IntelliJ shows types!

Text Blocks

String myMessage = “””“Hello, Java!”“””;
String myMessage = “”” “Hello, Java!” “””;
Illegal text block start: missing new line after opening quotes
String myMessage = “\”Hello, Java!\””;

Pattern Matching

if (obj instanceof String ) {    String s = (String)obj;}
if (obj instanceof String s) {
// your code here
}

Sealed classes

sealed interface Animal permits Reptile, Bird, Mammal, Fish {}

Record classes

record Person (int age, int height) {}

--

--

Responses (1)

Write a response