Short Overview:
Java 8 introduced functional programming features, Stream API, Lambda expressions, and many enhancements that changed how Java is written.
Major Java 8 Features
- Lambda Expressions
- Functional Interfaces
- Stream API
- Default & Static Methods in Interface
- Method References
- Optional Class
- Date and Time API
- forEach() Method
- Nashorn JavaScript Engine
1️⃣ Lambda Expressions
Short: Enables functional programming.
Listlist = Arrays.asList("Java","Spring"); list.forEach(s -> System.out.println(s));
Reduces boilerplate code.
2️⃣ Functional Interface
Short: Interface with exactly one abstract method.
@FunctionalInterface
interface MyInterface {
void show();
}
Built-in examples: Runnable, Comparator, Predicate.
3️⃣ Stream API
Short: Process collections functionally.
Listlist = Arrays.asList(1,2,3,4); list.stream() .filter(n -> n % 2 == 0) .forEach(System.out::println);
Operations:
- filter()
- map()
- sorted()
- collect()
4️⃣ Default & Static Methods in Interface
Short: Allows method implementation inside interface.
interface Test {
default void show() {
System.out.println("Default Method");
}
static void display() {
System.out.println("Static Method");
}
}
5️⃣ Method References
Short: Shortcut for lambda.
list.forEach(System.out::println);
Types:
- Class::staticMethod
- Object::instanceMethod
- Class::new
6️⃣ Optional Class
Short: Avoid NullPointerException.
Optionalname = Optional.ofNullable(null); System.out.println(name.orElse("Default"));
7️⃣ New Date and Time API
Short: Modern date/time handling.
LocalDate date = LocalDate.now(); LocalDateTime time = LocalDateTime.now();
Classes:
- LocalDate
- LocalTime
- LocalDateTime
- ZonedDateTime
8️⃣ forEach() Method
list.forEach(System.out::println);
Frequently Asked Interview Questions (With Answers)
1️⃣ What is Lambda Expression?
Anonymous function that provides implementation of functional interface.
2️⃣ What is Functional Interface?
Interface with one abstract method.
3️⃣ Difference between map() and flatMap()?
map() transforms elements; flatMap() flattens nested structures.
4️⃣ What is Optional used for?
To avoid NullPointerException.
5️⃣ Difference between default and static methods in interface?
Default method can be overridden; static cannot.
6️⃣ What is Stream?
Sequence of elements supporting functional operations.
7️⃣ What is method reference?
Short form of lambda using :: operator.
Advanced Interview Points
- Streams are lazy in nature.
- Intermediate vs Terminal operations.
- Parallel streams improve performance.
- Optional is container object.
- Functional programming introduced in Java 8.
0 comments