Əsas məzmuna keçin

Java 8-dən Sonrakı Yeni Xüsusiyyətlər

Java 8 (2014) - LTS​

Lambda İfadələri​

// Əvvəl
Runnable r1 = new Runnable() {
public void run() { System.out.println("Hello"); }
};

// İndi
Runnable r2 = () -> System.out.println("Hello");

Stream API​

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
int sum = numbers.stream()
.filter(n -> n % 2 == 0)
.mapToInt(Integer::intValue)
.sum();

Optional Class​

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

Default Methods​

interface Vehicle {
default void start() {
System.out.println("Starting engine...");
}
}

Date/Time API​

LocalDate today = LocalDate.now();
LocalDateTime now = LocalDateTime.now();
ZonedDateTime zoned = ZonedDateTime.now();

Java 9 (2017)​

Module System (Project Jigsaw)​

// module-info.java
module com.example.myapp {
requires java.base;
exports com.example.myapp.api;
}

Factory Methods for Collections​

List<String> list = List.of("A", "B", "C");
Set<Integer> set = Set.of(1, 2, 3);
Map<String, Integer> map = Map.of("key1", 1, "key2", 2);

Try-with-resources Enhancement​

// Java 9
FileInputStream fis = new FileInputStream("file.txt");
try (fis) {
// istifadə et
}

Private Methods in Interfaces​

interface MyInterface {
default void method1() {
commonLogic();
}

private void commonLogic() {
System.out.println("Common logic");
}
}

Java 10 (2018)​

Local Variable Type Inference (var)​

var list = new ArrayList<String>();
var map = new HashMap<String, Integer>();
var str = "Hello World";

// for loop-da
for (var item : list) {
System.out.println(item);
}

Application Class-Data Sharing​

CDS performansı artırır.

Java 11 (2018) - LTS​

String Methods​

String str = "  Hello World  ";
System.out.println(str.strip()); // "Hello World"
System.out.println(str.isBlank()); // false
System.out.println("".isBlank()); // true

String multiline = "Line1\nLine2\nLine3";
multiline.lines().forEach(System.out::println);

String repeated = "Java ".repeat(3); // "Java Java Java "

HTTP Client API​

HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://api.example.com"))
.build();

HttpResponse<String> response = client.send(request,
HttpResponse.BodyHandlers.ofString());

Running Single-File Programs​

java HelloWorld.java  # No compilation needed

Java 12 (2019)​

Switch Expressions (Preview)​

// Old style
String day = "MONDAY";
int numLetters;
switch (day) {
case "MONDAY":
case "FRIDAY":
case "SUNDAY":
numLetters = 6;
break;
default:
numLetters = 0;
}

// New style
int numLetters = switch (day) {
case "MONDAY", "FRIDAY", "SUNDAY" -> 6;
case "TUESDAY" -> 7;
default -> 0;
};

Java 13 (2019)​

Text Blocks (Preview)​

// Old
String html = "<html>\n" +
" <body>\n" +
" <p>Hello World</p>\n" +
" </body>\n" +
"</html>";

// New
String html = """
<html>
<body>
<p>Hello World</p>
</body>
</html>
""";

Java 14 (2020)​

Switch Expressions (Standard)​

Switch expressions artıq preview deyil.

Pattern Matching for instanceof (Preview)​

// Old
if (obj instanceof String) {
String s = (String) obj;
System.out.println(s.length());
}

// New
if (obj instanceof String s) {
System.out.println(s.length());
}

Records (Preview)​

public record Person(String name, int age) {
// Automatically generates:
// - constructor
// - getters (name(), age())
// - equals(), hashCode(), toString()
}

Person person = new Person("Ali", 25);
System.out.println(person.name()); // Ali

Java 15 (2020)​

Text Blocks (Standard)​

Sealed Classes (Preview)​

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

public final class Circle extends Shape { }
public final class Rectangle extends Shape { }
public final class Triangle extends Shape { }

Java 16 (2021)​

Pattern Matching for instanceof (Standard)​

Records (Standard)​

Java 17 (2021) - LTS​

Sealed Classes (Standard)​

Enhanced Pseudo-Random Number Generators​

RandomGenerator generator = RandomGenerator.of("Xoroshiro128PlusPlus");
int randomInt = generator.nextInt();

Java 18 (2022)​

Simple Web Server​

jwebserver -p 8080 -d /path/to/directory

UTF-8 by Default​

UTF-8 default charset oldu.

Java 19 (2022)​

Virtual Threads (Preview)​

Thread.ofVirtual().start(() -> {
System.out.println("Virtual thread");
});

// ExecutorService ilə
try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
executor.submit(() -> {
System.out.println("Task in virtual thread");
});
}

Pattern Matching for switch (Preview)​

String result = switch (obj) {
case Integer i -> "Integer: " + i;
case String s -> "String: " + s;
case null -> "null value";
default -> "Unknown type";
};

Java 20 (2023)​

Scoped Values (Incubator)​

Thread-local variables-ın yaxşılaşdırılmış versiyası.

Java 21 (2023) - LTS​

Virtual Threads (Standard)​

Pattern Matching for switch (Standard)​

Sequenced Collections​

// SequencedCollection interface
List<String> list = new ArrayList<>();
String first = list.getFirst();
String last = list.getLast();

list.addFirst("first");
list.addLast("last");

Tövsiyələr​

LTS Versiyaları​

  • Java 8 (2014) - Hələ də geniş istifadə olunur
  • Java 11 (2018) - Məsləhət görülür
  • Java 17 (2021) - Ən yeni LTS
  • Java 21 (2023) - Ən son LTS

Migration Strategy​

  1. Java 8 → 11: Module system, HTTP client
  2. Java 11 → 17: Records, sealed classes, text blocks
  3. Java 17 → 21: Virtual threads, pattern matching

Performance İmprovement​

Hər versiyada JVM performansı yaxşılaşır:

  • Garbage Collection təkmilləşmələri
  • JIT Compiler optimallaşdırmaları
  • Memory istifadəsinin azaldılması
  • Startup time yaxşılaşdırılması