Skip to content

Latest commit

 

History

History
49 lines (36 loc) · 1.9 KB

File metadata and controls

49 lines (36 loc) · 1.9 KB

Java 8 Features

A structured repository demonstrating the key features introduced in Java 8, implemented through hands-on examples. Each package focuses on one specific concept for clarity and easy navigation.


Concepts Covered

# Feature Package
1 Functional Interfaces functional_interfaces/
2 Lambda Expressions lambdas/
3 Built-in Functional Interfaces (Consumer, Predicate) functional_interfaces/
4 Interface Inheritance & Non-Functional Interfaces functional_interfaces/
5 Default & Static Methods in Interfaces default_methods/
6 Multithreading with Lambda (Runnable) lambdas/

Concepts in Detail

Functional Interfaces & Lambdas

A Functional Interface is any interface with exactly one abstract method, annotated with @FunctionalInterface. Java 8 allows these to be implemented using lambda expressions instead of verbose anonymous inner classes.

// Traditional way
Interface obj = new Interface() {
    public void incomplete() {
        System.out.println("implemented");
    }
};

// Java 8 Lambda way
Interface obj = () -> System.out.println("implemented");

Interface Inheritance

An interface can extend another interface and still remain a functional interface, as long as it does not add any new abstract methods. Adding even one new abstract method breaks the @FunctionalInterface contract.

Built-in Functional Interfaces

Java 8 ships with ready-to-use functional interfaces in java.util.function:

  • Consumer<T> — takes input, returns nothing
  • Predicate<T> — takes input, returns boolean
  • Function<T, R> — takes input, returns a result
  • Supplier<T> — no input, returns a result

Multithreading with Lambda

Java 8 lambdas simplify thread creation by allowing Runnable to be expressed as a single-line lambda, eliminating the need to create a separate class.