Free tutorials & notes in Hindi & English · Clean code examples · Mobile friendly learning
📘 Lesson  ·  Lesson 43

Java 8 Features

Written and reviewed by · Senior IT Faculty · 15+ years’ experience

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.

Java 8 ने क्या जोड़ा

Java 8 (2014 में release) Java के इतिहास के सबसे बड़े updates में से एक था। इसने Java में functional programming लाया, जिससे code छोटा, साफ़ और पढ़ने में आसान बनता है। Java 8 से पहले साधारण काम के लिए भी बहुत lines और anonymous classes चाहिए होती थीं; Java 8 के बाद वही काम एक line में हो जाता है।

जो पाँच 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
// पुराना तरीका (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();
▶ Output
Hello

2. Functional Interfaces

Functional interface वह interface है जिसमें ठीक एक abstract method हो। Lambdas सिर्फ़ functional interfaces के साथ काम करते हैं। Java इन्हें @FunctionalInterface से mark करता है। आम built-in: Runnable, Comparator, Predicate, Function

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

3. Stream API

Stream API से आप collection (जैसे list) को operations की pipeline की तरह process करते हैं — filter, map, sort, collect — बिना loop लिखे। Streams lazy और readable होती हैं।

Java
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);
▶ Output
[4, 8, 12]

4. Default Methods

Java 8 से पहले interfaces में method body नहीं हो सकती थी। Default methods default keyword से interface को तैयार implementation देने देती हैं। इससे Java पुराने interfaces में नई methods बिना existing code तोड़े जोड़ सका।

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

// Demo को hello() मुफ़्त मिल जाता है
new Demo().hello();
▶ Output
Hello from default method

5. Optional

Optional एक container है जिसमें value हो भी सकती है और नहीं भी। यह आपको "no value" वाली स्थिति संभालने पर मजबूर करके NullPointerException से बचाता है।

Java
import java.util.Optional;

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

सारांश Table

FeatureकामKeyword / Class
Lambdaछोटा anonymous function->
Functional Interfaceएक abstract method वाली interface@FunctionalInterface
Stream APICollections को pipeline में process.stream()
Default MethodInterface में method bodydefault
OptionalNull 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 से बचाता है।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

Java 8 की मुख्य features क्या हैं?
Java 8 की मुख्य features हैं: lambda expressions, functional interfaces, Stream API, interfaces में default methods, और Optional class.
Java में lambda expression क्या है?
Lambda expression एक छोटा anonymous function है जो arrow syntax (parameters) -> body से लिखा जाता है।
Stream और Collection में क्या अंतर है?
Collection data store करती है, जबकि Stream data process करती है और सिर्फ़ एक बार use हो सकती है।
← Back to Java Tutorial
🔗

Share this topic with a friend

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

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

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

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.