Junior — Middle
What major changes and new features were introduced in Java version 8?
sobes.tech AI
Answer from AI
Java 8 introduced several key changes and new features:
- Lambda expressions — allow writing more concise and expressive functional code.
- Stream API — for convenient working with collections, supporting filtering, mapping, aggregation, and parallel processing.
- Interfaces with default methods — enable adding methods with implementations in interfaces without breaking existing code.
- New date and time API (java.time) — more convenient and safer compared to the outdated Date and Calendar.
- Optional — a class for handling the possible absence of a value, helping to avoid NullPointerException.
- Performance improvements and JVM enhancements — such as improvements in garbage collection.
Example of lambda expression and Stream API:
List<String> names = Arrays.asList("Anna", "Bob", "Charlie");
names.stream()
.filter(name -> name.startsWith("A"))
.forEach(System.out::println); // Will output "Anna"