Java 8 Features
Written and reviewed by Gagan Bhardwaj · Senior IT Faculty · 15+ years’ experience
What Java 8 Added
The five features you must know are: lambda expressions, functional interfaces, the Stream API, default methods, and Optional. Let's see each with a working example.
1. Lambda Expressions
A lambda expression is a short, anonymous function — a block of code you can pass around like a value. It removes the need for bulky anonymous classes. The syntax is (parameters) -> { body }.
// Old way (before Java 8) Runnable r1 = new Runnable() { public void run() { System.out.println("Hello"); } }; // Java 8 lambda — one line Runnable r2 = () -> System.out.println("Hello"); r2.run();
Hello
2. Functional Interfaces
A functional interface is an interface with exactly one abstract method. Lambdas can only be used with functional interfaces. Java marks them with the @FunctionalInterface annotation. Common built-in ones are Runnable, Comparator, Predicate and Function.
// Predicate takes a value and returns true/false import java.util.function.Predicate; Predicate<Integer> isEven = n -> n % 2 == 0; System.out.println(isEven.test(10)); // true System.out.println(isEven.test(7)); // false
true false
3. Stream API
The Stream API lets you process a collection (like a list) as a pipeline of operations — filter, map, sort, collect — without writing loops. Streams are lazy and readable.
import java.util.*; import java.util.stream.*; List<Integer> nums = Arrays.asList(1, 2, 3, 4, 5, 6); // keep even numbers, double them, collect to a list List<Integer> result = nums.stream() .filter(n -> n % 2 == 0) .map(n -> n * 2) .collect(Collectors.toList()); System.out.println(result);
[4, 8, 12]
4. Default Methods
Before Java 8, interfaces could not have method bodies. Default methods allow an interface to provide a ready-made implementation using the default keyword. This let Java add new methods to old interfaces without breaking existing code.
interface Greet { default void hello() { System.out.println("Hello from default method"); } } class Demo implements Greet {} // Demo gets hello() for free new Demo().hello();
Hello from default method
5. Optional
Optional is a container that may or may not hold a value. It helps you avoid the dreaded NullPointerException by forcing you to handle the "no value" case explicitly.
import java.util.Optional; Optional<String> name = Optional.ofNullable(null); System.out.println(name.orElse("Guest")); // safe default
Guest
Quick Summary Table
| Feature | Purpose | Keyword / Class |
|---|---|---|
| Lambda | Short anonymous function | -> |
| Functional Interface | Interface with one abstract method | @FunctionalInterface |
| Stream API | Process collections in a pipeline | .stream() |
| Default Method | Method body inside an interface | default |
| Optional | Avoid null pointer errors | Optional |
Lambdas + streams together let you replace long for loops with one readable line. This is now the standard style in modern Java and is a very common interview topic.
A stream can be used only once. After a terminal operation like collect(), you cannot reuse the same stream — create a new one from the source.
Summary
- Java 8 (2014) added functional programming to Java.
- Lambda = short anonymous function using
->. - Functional interface = one abstract method; enables lambdas.
- Stream API processes collections as a pipeline (filter, map, collect).
- Default methods add bodies to interfaces; Optional avoids null errors.
Java 8 ने क्या जोड़ा
जो पाँच features ज़रूर आने चाहिए: lambda expressions, functional interfaces, Stream API, default methods, और Optional। हर एक को उदाहरण के साथ देखते हैं।
1. Lambda Expressions
Lambda expression एक छोटा, anonymous function है — code का एक टुकड़ा जिसे आप value की तरह pass कर सकते हैं। इससे भारी-भरकम anonymous classes की ज़रूरत खत्म हो जाती है। Syntax: (parameters) -> { body }।
// पुराना तरीका (Java 8 से पहले) Runnable r1 = new Runnable() { public void run() { System.out.println("Hello"); } }; // Java 8 lambda — एक line Runnable r2 = () -> System.out.println("Hello"); r2.run();
Hello
2. Functional Interfaces
Functional interface वह interface है जिसमें ठीक एक abstract method हो। Lambdas सिर्फ़ functional interfaces के साथ काम करते हैं। Java इन्हें @FunctionalInterface से mark करता है। आम built-in: Runnable, Comparator, Predicate, Function।
// Predicate एक value लेकर true/false देता है import java.util.function.Predicate; Predicate<Integer> isEven = n -> n % 2 == 0; System.out.println(isEven.test(10)); // true System.out.println(isEven.test(7)); // false
true false
3. Stream API
Stream API से आप collection (जैसे list) को operations की pipeline की तरह process करते हैं — filter, map, sort, collect — बिना loop लिखे। Streams lazy और readable होती हैं।
import java.util.*; import java.util.stream.*; List<Integer> nums = Arrays.asList(1, 2, 3, 4, 5, 6); // even numbers रखो, double करो, list में collect करो List<Integer> result = nums.stream() .filter(n -> n % 2 == 0) .map(n -> n * 2) .collect(Collectors.toList()); System.out.println(result);
[4, 8, 12]
4. Default Methods
Java 8 से पहले interfaces में method body नहीं हो सकती थी। Default methods default keyword से interface को तैयार implementation देने देती हैं। इससे Java पुराने interfaces में नई methods बिना existing code तोड़े जोड़ सका।
interface Greet { default void hello() { System.out.println("Hello from default method"); } } class Demo implements Greet {} // Demo को hello() मुफ़्त मिल जाता है new Demo().hello();
Hello from default method
5. Optional
Optional एक container है जिसमें value हो भी सकती है और नहीं भी। यह आपको "no value" वाली स्थिति संभालने पर मजबूर करके NullPointerException से बचाता है।
import java.util.Optional; Optional<String> name = Optional.ofNullable(null); System.out.println(name.orElse("Guest")); // safe default
Guest
सारांश Table
| Feature | काम | Keyword / Class |
|---|---|---|
| Lambda | छोटा anonymous function | -> |
| Functional Interface | एक abstract method वाली interface | @FunctionalInterface |
| Stream API | Collections को pipeline में process | .stream() |
| Default Method | Interface में method body | default |
| Optional | Null pointer errors से बचाव | Optional |
Lambdas + streams मिलकर लंबे for loops को एक readable line में बदल देते हैं। यह अब modern Java का standard तरीका है और interview का बहुत common topic है।
एक stream सिर्फ़ एक बार इस्तेमाल हो सकती है। collect() जैसे terminal operation के बाद उसी stream को दोबारा use नहीं कर सकते — source से नई stream बनाएं।
सारांश
- Java 8 (2014) ने Java में functional programming जोड़ा।
- Lambda =
->से बना छोटा anonymous function। - Functional interface = एक abstract method; lambdas को enable करता है।
- Stream API collections को pipeline में process करती है (filter, map, collect)।
- Default methods interfaces में body जोड़ती हैं; Optional null errors से बचाता है।