Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
📘 Lesson  ·  Lesson 43

Java 8 Features

In this lesson: Top Java 8 features — lambda expressions, Stream API, functional interfaces, Optional, default methods — examples.

What Java 8 Added

Java 8 (released in 2014) was one of the biggest updates in Java's history. It brought functional programming to Java, letting you write shorter, cleaner and more readable code. Before Java 8 you needed many lines and anonymous classes to do simple things; after Java 8 the same work fits in a single line.

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 }.

Java
// 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();
▶ Output
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.

Java
// 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
▶ Output
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.

Java
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);
▶ Output
[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.

Java
interface Greet {
    default void hello() {
        System.out.println("Hello from default method");
    }
}
class Demo implements Greet {}

// Demo gets hello() for free
new Demo().hello();
▶ Output
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.

Java
import java.util.Optional;

Optional<String> name = Optional.ofNullable(null);
System.out.println(name.orElse("Guest"));  // safe default
▶ Output
Guest

Quick Summary Table

FeaturePurposeKeyword / Class
LambdaShort anonymous function->
Functional InterfaceInterface with one abstract method@FunctionalInterface
Stream APIProcess collections in a pipeline.stream()
Default MethodMethod body inside an interfacedefault
OptionalAvoid null pointer errorsOptional
💡 Why it matters

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.

⚠️ Common mistake

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.

Frequently Asked Questions

What are the main features of Java 8?
The main Java 8 features are lambda expressions, functional interfaces, the Stream API, default methods in interfaces, and the Optional class. Together they bring functional programming to Java.
What is a lambda expression in Java?
A lambda expression is a short anonymous function written with the arrow syntax (parameters) -> body. It replaces bulky anonymous classes and can be used wherever a functional interface is expected.
What is the difference between a Stream and a Collection?
A Collection stores data, while a Stream processes data. A stream does not hold elements; it carries them through a pipeline of operations like filter and map, and can be used only once.
Why was the Optional class introduced?
Optional was introduced to reduce NullPointerException errors. It represents a value that may or may not be present and forces the programmer to handle the empty case explicitly.
← Back to Java Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicLambda Expressions Your progress: 43 / 61 (70%) · Lesson 44 / 61 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free