Home RoadMap Blog Contact us Learn

9.Core Java Concepts: Java 8 Features

Core Java Concepts: Java 8 Features

Core Java Concepts for Selenium Automation: Java 8 Features

Java 8 introduced several new features that enhance productivity and functional programming. This section covers the most important features like lambda expressions, functional interfaces, method references, default methods, and the Optional class.

1. Lambda Expressions

Lambda expressions allow you to write anonymous methods in a concise way, making your code more readable. They are commonly used in functional programming with the Stream API and Collections.

// Example of a lambda expression
List<String> names = Arrays.asList("Alice", "Bob", "Charlie");

names.forEach(name -> System.out.println(name));  // Using lambda

2. Functional Interfaces

Functional interfaces are interfaces with exactly one abstract method. Some commonly used functional interfaces in Java 8 are:

  • Predicate<T> - represents a boolean-valued function
  • Consumer<T> - represents an operation that takes a single input and returns no result
  • Function<T, R> - represents a function that accepts one argument and produces a result

Example: Using Predicate Functional Interface

Predicate<Integer> isEven = number -> number % 2 == 0;

System.out.println(isEven.test(4));  // Output: true
System.out.println(isEven.test(5));  // Output: false

3. Method References

Method references are a shorthand for calling a method from a class or object. They can be used to improve the readability of lambda expressions.

// Example of method reference
List<String> names = Arrays.asList("Alice", "Bob", "Charlie");

names.forEach(System.out::println);  // Method reference to System.out.println

4. Default and Static Methods in Interfaces

Java 8 introduced default and static methods in interfaces, allowing you to add methods in interfaces without breaking existing implementations.

Default Method Example:

interface MyInterface {
    default void defaultMethod() {
        System.out.println("This is a default method.");
    }
}

class MyClass implements MyInterface {
    public static void main(String[] args) {
        MyClass obj = new MyClass();
        obj.defaultMethod();  // Output: This is a default method.
    }
}

Static Method Example:

interface MyInterface {
    static void staticMethod() {
        System.out.println("This is a static method.");
    }
}

public class MyClass {
    public static void main(String[] args) {
        MyInterface.staticMethod();  // Output: This is a static method.
    }
}

5. Optional Class

The Optional class is used to handle null values more gracefully, preventing NullPointerException. It is a container object which may or may not contain a non-null value.

Example: Using Optional Class

Optional<String> optional = Optional.ofNullable(null);

if (optional.isPresent()) {
    System.out.println(optional.get());
} else {
    System.out.println("Value is absent");
}
// Output: Value is absent
Quick Exercise: Use lambda expressions to filter out even numbers from a list and print them using method references.

Conclusion

Java 8 features bring powerful improvements to Java, especially in terms of functional programming and handling collections. These features are highly useful in modern Selenium automation projects to make the code more concise and readable.

Java 8 Features

Recent Posts