0% found this document useful (0 votes)
6 views4 pages

Java 8 Features

The document explains Functional Interfaces and Lambda Expressions in Java 8, highlighting that a functional interface contains exactly one abstract method and enables the use of lambda expressions. It details the syntax and benefits of lambda expressions, along with examples comparing traditional and modern coding styles. Additionally, it outlines several built-in functional interfaces provided by Java, including Predicate, Function, Consumer, and Supplier, along with their use cases.

Uploaded by

aravaramana0
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

Java 8 Features

The document explains Functional Interfaces and Lambda Expressions in Java 8, highlighting that a functional interface contains exactly one abstract method and enables the use of lambda expressions. It details the syntax and benefits of lambda expressions, along with examples comparing traditional and modern coding styles. Additionally, it outlines several built-in functional interfaces provided by Java, including Predicate, Function, Consumer, and Supplier, along with their use cases.

Uploaded by

aravaramana0
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

You said:

functional interfaces and lambda expressions in Java 8 features.

ChatGPT said:

Below is a clear, interview-ready explanation of Functional Interfaces and Lambda


Expressions in Java 8, with step-by-step concepts, syntax, diagrams, and examples.

1️⃣ Functional Interface (Java 8)

🔹 What is a Functional Interface?

A functional interface is an interface that contains exactly one abstract method.

✔ Java 8 introduced functional interfaces to support Lambda Expressions.

🔹 Key Rules

 Only one abstract method

 Can have:

 default methods

 static methods

 Annotated with @FunctionalInterface (optional but recommended)

🔹 Example

@FunctionalInterface interface Calculator { int add(int a, int b); // single abstract method }

🔹 Why Functional Interface?

➡ Enables lambda expressions


➡ Supports functional programming
➡ Reduces boilerplate code

🔹 Diagram (Conceptual)

Functional Interface
|

| (one abstract method)

Lambda Expression

2️⃣ Lambda Expression

🔹 What is a Lambda Expression?

A lambda expression is an anonymous function that:

 Has no name

 No return type (inferred)

 No access modifier

🔹 Syntax

(parameters) -> expression

or

(parameters) -> { statements }

🔹 Example Without Lambda (Old Style)

Calculator c = new Calculator() { public int add(int a, int b) { return a + b; } };

🔹 Same Code With Lambda (Java 8)

Calculator c = (a, b) -> a + b; [Link]([Link](10, 20));

✔ Clean
✔ Short
✔ Readable

3️⃣ Built-in Functional Interfaces (Java 8)

Java provides predefined functional interfaces in:

[Link]
1️⃣ Predicate<T>

✔ Takes one input


✔ Returns boolean

Predicate<Integer> p = n -> n > 18; [Link]([Link](20)); // true

📌 Use case: Conditions, filtering

2️⃣ Function<T, R>

✔ Takes input → returns output

Function<String, Integer> f = s -> [Link](); [Link]([Link]("Java")); // 4

📌 Use case: Transform data

3️⃣ Consumer<T>

✔ Takes input
✔ No return value

Consumer<String> c = s -> [Link](s); [Link]("Hello Java");

📌 Use case: Printing, logging

4️⃣ Supplier<T>

✔ No input
✔ Returns output

Supplier<Double> s = () -> [Link](); [Link]([Link]());

📌 Use case: Object creation, lazy values

🔹 Summary Table

Interface Input Output

Predicate 1 boolean

Function 1 value
Interface Input Output

Consumer 1 no return

Supplier 0 value

You might also like