Main Features Introduced With Java8
18 min readJul 17, 2024
This article explores the new features introduced in Java 8. Future articles will cover features from other Java versions.
Java 8 — (18/03/2014; LTS)
Lambda Expressions:
- Enable to treat functionality as a method argument
- Lambda expressions can be used to pass functionality as an argument to a method. This is particularly useful in functional interfaces like
Comparator
,Runnable
, etc.
import java.util.Arrays;
import java.util.List;
public class LambdaExample {
public static void main(String[] args) {
List<String> names = Arrays.asList("John", "Alice", "Bob");
// Using lambda expression to sort the list
names.sort((String a, String b) -> a.compareTo(b));
System.out.println(names);
}
}
2. Enable to treat a code as data.
- Lambda expressions allow you to treat code as data by passing a block of code that can be executed at a later time.
@FunctionalInterface
interface MathOperation {
int operate(int a, int b);
}
public class LambdaExample {
public static void main(String[] args) {
// Using lambda expressions to define operations
MathOperation addition = (a, b) -> a + b;
MathOperation subtraction = (a, b) -> a - b…