Java 8 Features Overview
In this page:
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
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
}
}
Login to try C/C++/Java/PHP code in the editor
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
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
}
}
Login to try C/C++/Java/PHP code in the editor
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
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);
}
}
Login to try C/C++/Java/PHP code in the editor
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
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);
}
}
Login to try C/C++/Java/PHP code in the editor
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
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 try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first: