Java 8, released in March 2014, introduced a significant number of new features and enhancements aimed at improving productivity, performance, security, and ease of use. Here are some of the most notable features:
1. Lambda Expressions:
Explanation: A Lambda Expression is a short, anonymous function introduced in Java 8 that enables functional programming in Java. It allows you to write inline implementations of functional interfaces (interfaces with a single abstract method), making your code shorter, cleaner, and more expressive..
Example1:
// Traditional way
Runnable r1 = new Runnable() {
@Override
public void run() {
System.out.println("Hello world one!");
}
};
// Using lambda expression
Runnable r2 = () -> System.out.println("Hello world two!");
Example2:
List<String> list = Arrays.asList("a", "b", "c");
list.forEach(element -> System.out.println(element));
Example3:
import java.util.stream.IntStream;
public class LambdaPrintExample {
public static void main(String[] args) {
IntStream.rangeClosed(1, 100)
.forEach(n -> System.out.println(n));
(OR)
List<Integer> numbers = IntStream.rangeClosed(1, 100)
.boxed() // Convert IntStream to Stream<Integer>
.collect(Collectors.toList());
// Print each number using a lambda
numbers.forEach(n -> System.out.println(n));
}
}
🧠Summary
Feature | Lambda Expression Example |
---|---|
No arguments | () -> System.out.println("Hi") |
One argument | x -> x * x |
Multiple arguments | (a, b) -> a + b |
With block | (a, b) -> { return a * b; } |
2.Functional Interfaces:
Explanation: A functional interface is an interface that contains only one abstract method, annotated with @FunctionalInterface
. It can have multiple default or static methods. They can be used with lambda expressions and method references.
Example
✅ Example: Custom Functional Interface
✅ Usage with Lambda:
✅ Common Built-in Functional Interfaces (in java.util.function
)
Interface | Method | Use Case | Lambda Example |
---|---|---|---|
Predicate<T> | test(T) | Returns boolean | x -> x > 10 |
Function<T, R> | apply(T) | Converts type T to type R | s -> s.length() |
Consumer<T> | accept(T) | Takes a value and returns nothing | x -> System.out.println(x) |
Supplier<T> | get() | Provides a value | () -> "Hello" |
UnaryOperator<T> | apply(T) | Operates on a single type T | x -> x * x |
BinaryOperator<T> | apply(T, T) | Operates on two values of same type | (a, b) -> a + b |
✅ Examples:
🔹 Predicate<T>
:
🔹 Function<T, R>
:
🔹 Consumer<T>
:
🔹 Supplier<T>
:
✅ Why Use Functional Interfaces?
-
Enable cleaner, more expressive code using lambdas.
-
Power the Stream API, event handlers, concurrency utilities, etc.
-
Provide building blocks for custom behavior injection.
3.Stream API:
Explanation: The Stream API provides a powerful way to process sequences of elements (such as collections) in a functional style. Streams support operations like map, filter, reduce, and more.
Example1:
List<String> myList = Arrays.asList("a1", "a2", "b1", "c2", "c1");
myList
.stream()
.filter(s -> s.startsWith("c"))
.map(String::toUpperCase)
.sorted()
.forEach(System.out::println);
Example2:
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
List<Integer> squares = numbers
.stream()
.map(n -> n * n)
.collect(Collectors.toList());
4.Default Methods:
Explanation: Default methods allow you to add new methods to interfaces without breaking the existing implementations. They provide a default implementation that can be overridden by implementing classes.
Example:
interface MyInterface {
default void newMethod() {
System.out.println("Newly added default method");
}
void existingMethod();
}
5.Optional Class:
Explanation: The Optional
class in Java is a container object introduced in Java 8 to avoid NullPointerException
by representing the presence or absence of a value in a type-safe way.
✅ Why use Optional
?
Traditionally:
With Optional
:
✅ Creating Optional
Instances:
6.Method References:
Explanation: Method references provide a way to refer to methods without executing them. They are a shorthand for lambda expressions that only call an existing method.
Example
// Using lambda expression
List<String> names = Arrays.asList("Swamy", "Java", "Spring");
list.forEach(item -> System.out.println(item));
// Using method reference
list.forEach(System.out::println);
7.Date and Time API:
Explanation: Java 8 introduced a new Date and Time API (java.time
package) that is more comprehensive model for date and time that is more readable and less error-prone than the previous java.util.Date
and java.util.Calendar
classes.
✅ Key Features of java.time
-
Immutable and thread-safe
-
Fluent and easy-to-read API
-
Clear separation of date, time, date-time, instant, and zone
-
Better time zone support
-
Built-in parsing and formatting
-
Inspired by the Joda-Time library
Example
LocalDate date = LocalDate.now(); // e.g. 2025-06-19
LocalTime time = LocalTime.now(); // e.g. 09:41:32.123
LocalDateTime dateTime = LocalDateTime.now(); // e.g. 2025-06-19T09:41:32.123
8. Nashorn JavaScript Engine
Explanation: A JavaScript engine for executing embedded JavaScript code within Java applications. Provides improved performance and compliance with ECMAScript standards.
Example
9. Base64 Encoding and Decoding
Explanation: Java 8 introduced the java.util.Base64
class for encoding and decoding data in Base64 format.
Example
10. Parallel Array Sorting
Explanation: Java 8 introduced the Arrays.parallelSort()
method, which uses the Fork/Join framework to sort arrays in parallel.
Example
11. Stamps and Counters
Explanation: New classes introduced for concurrent programming, such as StampedLock
and LongAdder
.
Example:
12. Concurrent Adders
Explanation: LongAdder
and DoubleAdder
are new classes that provide better performance in highly-concurrent scenarios.
Example
13. PermGen Removal
- Explanation: The permanent generation (PermGen) space was removed and replaced with Metaspace, which automatically grows as needed.
- Impact: Reduces OutOfMemoryError related to PermGen space and improves performance.
14. JavaFX Enhancements
- Explanation: JavaFX was enhanced with new UI controls, 3D graphics features, and new packaging tools.
- Example:
15. Repeating Annotations
- Explanation: Java 8 allows the same annotation to be used multiple times on the same declaration or type use.
- Example: