← Back to Advanced Java Course | Chapter 4: Modern Java Features | Lesson 1 of 12

Java 8 Features Overview

Default Methods in Interfaces

Java 8 allows you to add a concrete method implementation directly inside an interface using the default keyword. This lets library authors extend existing interfaces with new methods without breaking every class that already implements them, since implementers automatically inherit the default behavior.

Example: Default Methods in Interfaces

java
public class Main {
	interface Vehicle {
		default void honk() {
			System.out.println("Beep beep!");
		}
	}
	static class Car implements Vehicle {}
	public static void main(String[] args) {
		new Car().honk(); // inherited default behavior
	}
}

Static Methods in Interfaces

You can also define static helper methods directly inside interfaces alongside their abstract and default ones. These static methods belong to the interface itself, are called through the interface name, and cannot be overridden by any implementing class.

Example: Static Methods in Interfaces

java
public class Main {
	interface MathUtil {
		static int square(int n) {
			return n * n;
		}
	}
	public static void main(String[] args) {
		System.out.println(MathUtil.square(6)); // called through the interface name
	}
}

Stream and Lambda Synergy

Java 8 introduced Streams and Lambdas together, and they were designed hand-in-hand to let you write clean, declarative, functional-style pipelines over collections instead of manually written loops.

Example: Stream and Lambda Synergy

java
import java.util.*;
public class Main {
	public static void main(String[] args) {
		List<Integer> nums = List.of(1, 2, 3, 4, 5);
		nums.stream().filter(n -> n % 2 == 0).forEach(System.out::println);
	}
}

Local Date and Time API

The java.time package, introduced in Java 8, provides a modern, immutable, thread-safe date and time API. It was built specifically to replace the older java.util.Date and Calendar classes, whose mutability and confusing month-indexing had caused years of subtle bugs.

Example: Local Date and Time API

java
import java.time.LocalDate;
public class Main {
	public static void main(String[] args) {
		LocalDate date = LocalDate.of(2024, 1, 15);
		LocalDate nextWeek = date.plusDays(7);
		System.out.println(date + " -> " + nextWeek);
	}
}

Base64 Encoding

Java 8 also shipped a native Base64 utility class in java.util, making it straightforward to encode and decode data in Base64 format without pulling in a third-party library just for that one task.

Example: Base64 Encoding

java
import java.util.Base64;
public class Main {
	public static void main(String[] args) {
		String encoded = Base64.getEncoder().encodeToString("hello".getBytes());
		String decoded = new String(Base64.getDecoder().decode(encoded));
		System.out.println(encoded + " -> " + decoded);
	}
}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.