Skip to content

Latest commit

 

History

History
143 lines (93 loc) · 4.75 KB

File metadata and controls

143 lines (93 loc) · 4.75 KB

Java 8, released in March 2014, is widely regarded as the most revolutionary update in the history of the language. It marked the transition from a strictly imperative programming style to a functional one, allowing Java to remain competitive in an era of multi-core processors and big data.


🔴 Java 8 (LTS) - The Functional Revolution

1. Lambda Expressions (JEP 126)

This is the single most important feature of Java 8. It introduced "functions as first-class citizens," allowing you to pass logic as an argument to a method.

  • The Enhancement: Provides a clear and concise way to represent one-method interfaces (Functional Interfaces) using an expression.
  • The "Why": Before Java 8, if you wanted to pass logic, you had to use cumbersome "Anonymous Inner Classes." Lambdas removed that boilerplate.

Code Implementation

// Pre-Java 8: Anonymous Inner Class
Runnable r = new Runnable() {
    @Override
    public void run() {
        System.out.println("Hello World");
    }
};

// Java 8: Lambda Expression
Runnable r8 = () -> System.out.println("Hello World");

// Example with a List
List<String> names = Arrays.asList("Jack", "Jill", "John");
names.forEach(name -> System.out.println(name));

2. Streams API (JEP 107)

The Streams API changed how we process collections of data.

  • The Enhancement: A "Stream" is a sequence of elements supporting sequential and parallel aggregate operations.
  • The "Why": It allows for declarative data processing. Instead of writing complex for loops with nested if statements, you describe what you want to happen to the data.

Code Implementation

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);

// Filter even numbers, square them, and find the sum
int sum = numbers.stream()
    .filter(n -> n % 2 == 0)
    .map(n -> n * n)
    .reduce(0, Integer::sum); 

System.out.println(sum); // Output: 56 (4 + 16 + 36)

3. Optional Class

A new container object used to contain not-null objects.

  • The Enhancement: Optional<T> is used to represent the presence or absence of a value.
  • The "Why": It was designed to reduce the frequency of NullPointerException (NPE) and force the programmer to think about the "empty" case.

Code Implementation

// Instead of returning null
public Optional<String> getName(int id) {
    return (id == 1) ? Optional.of("Admin") : Optional.empty();
}

// Handling the result
getName(1).ifPresent(name -> System.out.println(name.toUpperCase()));
String name = getName(2).orElse("Guest");

4. Default Methods

For the first time, Java allowed interfaces to have method implementations.

  • The Enhancement: You can add new methods to interfaces using the default keyword without breaking the classes that already implement the interface.
  • The "Why": This was essential to evolve the Collections API (like adding the stream() method to the List interface) without forcing every developer in the world to rewrite their custom List implementations.

Code Implementation

public interface Vehicle {
    void drive();
    
    // New method added to existing interface
    default void honk() {
        System.out.println("Beep beep!");
    }
}

5. New Date and Time API (JEP 150)

The old java.util.Date and java.util.Calendar were notoriously difficult to use, thread-unsafe, and poorly designed.

  • The Enhancement: A new package java.time based on the Joda-Time library. It is immutable and thread-safe.
  • Key Classes: LocalDate, LocalTime, LocalDateTime, ZonedDateTime, and Duration.

Code Implementation

LocalDate today = LocalDate.now();
LocalDate nextWeek = today.plusWeeks(1);

LocalDateTime appointment = LocalDateTime.of(2024, Month.OCTOBER, 10, 14, 30);

6. PermGen to Metaspace (Memory Architecture)

A major internal shift in how the JVM handles memory.

  • The Shift: The "Permanent Generation" (PermGen) area of the heap was removed. It was replaced by Metaspace.
  • The Benefit: PermGen had a fixed size, often leading to java.lang.OutOfMemoryError: PermGen space. Metaspace is located in Native Memory and can grow automatically, making the JVM much more stable under heavy class-loading.

Java 8 Quick Summary Table

Feature Category Primary Benefit
Lambdas Language Functional programming and less boilerplate.
Streams API Powerful, readable data processing.
Optional API Better null safety and cleaner code.
Default Methods Language Interface evolution without breaking code.
Date/Time API API Thread-safe and intuitive date handling.
Metaspace JVM Improved memory stability and auto-scaling.