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

Java 8 Features Overview

Java 8 added a bundle of big upgrades such as lambdas, streams, and a new date system, like a major update to your phone. They made Java code shorter and cleaner.

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.* so it can be used by its short name
import java.util.*;
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		List<Integer> nums = List.of(1, 2, 3, 4, 5);
		// Create a stream from the collection to process it step by step
		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 so it can be used by its short name
import java.time.LocalDate;
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		LocalDate date = LocalDate.of(2024, 1, 15);
		LocalDate nextWeek = date.plusDays(7);
		// Print a line to the console
		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 so it can be used by its short name
import java.util.Base64;
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		String encoded = Base64.getEncoder().encodeToString("hello".getBytes());
		// Create a new String object and store it in decoded
		String decoded = new String(Base64.getDecoder().decode(encoded));
		// Print a line to the console
		System.out.println(encoded + " -> " + decoded);
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Ignoring the return value of methods like date.plusDays(1), since LocalDate is immutable and the original stays unchanged.
  2. Calling a static interface method through an instance or implementing class, like car.helper(), when it must be called as Vehicle.helper().
  3. Implementing two interfaces with the same default method and not overriding it, which is a compile error because Java cannot choose one.

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.