What's New in Java 8 – Complete Feature Overview
Version: Java 8 | Release Date: March 2014 | Type: Major Language Update
Java 8 introduced some of the most powerful and modern features to the language, bringing functional programming capabilities and cleaner syntax to developers. From lambdas to streams and a new date-time API, this version remains a milestone in Java’s evolution. Explore more Java tutorials.
Key Features Introduced in Java 8
- Lambda Expressions: Enable writing anonymous functions using `->` syntax.
- Streams API: Process collections functionally with `filter`, `map`, `reduce`, etc.
- Default & Static Methods: Allow interfaces to have method implementations.
- Optional Class: A safer container for null values.
- New Date/Time API: Thread-safe, modern API under `java.time` package.
- Functional Interfaces: Interfaces with a single abstract method, like `Runnable`, `Predicate`.
- Method References: Shorthand for calling methods via `Class::methodName` syntax.
- Nashorn JavaScript Engine: Run JavaScript code within Java (deprecated in later versions).
Examples
Lambda Expression
List<String> list = List.of("Java", "Python", "C++");
list.forEach(name -> System.out.println(name));
Stream API
list.stream()
.filter(name -> name.startsWith("J"))
.map(String::toUpperCase)
.forEach(System.out::println);
Optional Usage
Optional<String> opt = Optional.ofNullable("Java 8");
opt.ifPresent(System.out::println);
New Date/Time API
LocalDate date = LocalDate.now();
LocalTime time = LocalTime.now();
LocalDateTime dateTime = LocalDateTime.now();
Feature Summary Table
Feature | Description |
---|---|
Lambda Expressions | Functional syntax for passing behavior |
Streams API | Process collections with map/filter/reduce |
Optional | Wrapper to avoid null pointer exceptions |
Default Methods | Interfaces can include implementations |
Method References | Shorthand for lambda expressions |
New Date/Time API | Modern, immutable, and thread-safe |
Functional Interfaces | Enable use with lambda expressions |
What is Functional Programming?
Functional Programming (FP) is a programming paradigm that emphasizes writing software using pure functions, avoiding shared state, and relying on immutability. In FP, you describe what to do rather than how to do it.
- Pure Functions: Always return the same result for the same input with no side effects.
- Immutability: Data is never modified, only transformed into new data.
- Higher-Order Functions: Functions that take or return other functions.
- Function Composition: Building complex behavior by combining simple functions.
- Declarative Style: Focus on what the result should be, not the step-by-step logic.
Example: Declarative vs Imperative
// Imperative
List<String> result = new ArrayList<>();
for (String name : names) {
if (name.length() > 3) result.add(name);
}
// Functional (Declarative)
List<String> result = names.stream()
.filter(name -> name.length() > 3)
.collect(Collectors.toList());
Ready to go deeper with Java? Join our advanced Java series — new content added weekly!
Pure Function is a function that always gives the same output for the same input (Deterministic in nature) and has no side effects like modifying external variables or states.
✅ Example of a Pure Function:
int square(int x) {
return x * x;
}
This function always returns the same result and does not affect any external state.
❌ Example of an Impure (Normal) Function:
int counter = 0;
int incrementAndGet(int x) {
counter++; // modifies external state
return x + counter;
}
This function changes a global variable and produces different results even for the same input.
🔁 Key Differences:
- Pure: Predictable, easy to test, no side effects
- Impure: Depends on global state, harder to test and debug
🔁 Key Benefits:
- Pure: Predictable, easy to test, no side effects
- Impure: Depends on global state, harder to test and debug
Immutability means once an object is created, its state cannot be changed.
✅ Example: Immutable String
String original = "Hello";
String modified = original.concat(" World");
System.out.println(original); // "Hello"
System.out.println(modified); // "Hello World"
The original string remains unchanged. A new object is created with the modification.
🔒 Benefits of Immutability:
- Thread-safe without synchronization
- Easier to reason about state
- Used in functional programming for predictable behavior
First-Class Functions means functions can be:
- Assigned to variables
- Passed as arguments
- Returned from other functions
✅ Example using Java Lambda Expression
Function square = x -> x * x;
System.out.println(square.apply(5)); // 25
Here, the function is stored in a variable and invoked like data.
java.util.function
interfaces like Function
, Consumer
, and Predicate
.
Streams in Java 8+ are a powerful way to process collections using a **functional style**.
✅ Example:
List numbers = Arrays.asList(1, 2, 3, 4, 5);
List squaredEvens = numbers.stream()
.filter(n -> n % 2 == 0)
.map(n -> n * n)
.collect(Collectors.toList());
System.out.println(squaredEvens); // [4, 16]
filter()
– selects elementsmap()
– transforms elementscollect()
– gathers results
collect()
are invoked.
First-Class Functions mean that functions can be:
- Assigned to variables
- Passed as arguments
- Returned from other functions
Java supports this using Function
interface and lambda expressions.
✅ Complete Java Example:
import java.util.function.Function;
public class FirstClassFunctionsDemo {
public static void main(String[] args) {
// 1. Assigning function to a variable
Function<Integer, Integer> square = x -> x * x;
System.out.println("Square of 5: " + square.apply(5)); // 25
// 2. Passing function as argument
int result = applyFunction(10, x -> x + 100);
System.out.println("Result after applying function: " + result); // 110
// 3. Returning function from method
Function<Integer, Integer> doubler = getMultiplierFunction(2);
System.out.println("Doubled 8: " + doubler.apply(8)); // 16
}
public static int applyFunction(int value, Function<Integer, Integer> func) {
return func.apply(value);
}
public static Function<Integer, Integer> getMultiplierFunction(int multiplier) {
return x -> x * multiplier;
}
}
Function
, Predicate
, and Consumer
are the foundation for first-class functions in Java.
Frequently Asked Questions
Q1. What is the biggest feature of Java 8?
A: Lambda expressions and Streams API are the most impactful, bringing functional programming to Java.
Q2. Is Java 8 still supported?
A: Yes, it remains widely used and is officially supported by several vendors like Oracle (for extended support), Amazon Corretto, and others.