Skip to content

Latest commit

 

History

History
96 lines (70 loc) · 1.5 KB

JDK-features.md

File metadata and controls

96 lines (70 loc) · 1.5 KB

JDK

[[TOC]]

Java SE 5 (2004)

Generics

List<String> list = new ArrayList<>();
list.add("Hello");
String str = list.get(0);

Enumerated types (Enums)

public enum Day {
    SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
}

Varargs

public void printNumbers(int... numbers) {
    for (int number : numbers) {
        System.out.println(number);
    }
}

Java SE 7 (2011)

Try-with-resources Statement

try (BufferedReader br = new BufferedReader(new FileReader("file.txt"))) {
    System.out.println(br.readLine());
}

Java SE 8 (2014)

Lambda Expressions

List<String> list = Arrays.asList("a", "b", "c");
list.forEach(item -> System.out.println(item));

Functional Interfaces

@FunctionalInterface
interface MyFunctionalInterface {
    void myMethod();
}

Stream API

List<String> list = Arrays.asList("a", "b", "c");
list.stream().filter(s -> s.startsWith("a")).forEach(System.out::println);

Optional Class

Optional<String> optional = Optional.of("Hello");
optional.ifPresent(System.out::println);

New Date and Time API (java.time)

LocalDate date = LocalDate.now();
LocalTime time = LocalTime.now();
LocalDateTime dateTime = LocalDateTime.now();

Java SE 10 (2018)

Local-Variable Type Inference (var)

var list = new ArrayList<String>();
list.add("Hello");

Java SE 16 (2021)

Records

public record Point(int x, int y) {}