DEV Community

Manish Thakurani for CodeGreen

Posted on

What are new features of Java 8 #interviewQuestion

New Features in Java 8

Java 8 introduced several significant features:

  1. Lambda Expressions
    • Syntax: Lambda expressions enable functional programming.
    • Example:
List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
names.forEach(name -> System.out.println(name));
Enter fullscreen mode Exit fullscreen mode
  1. Stream API
    • Functional Operations: Stream API facilitates processing collections with functional-style operations.
    • Example:
List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
long count = names.stream().filter(name -> name.startsWith("A")).count();
Enter fullscreen mode Exit fullscreen mode
  1. Date and Time API
    • Improved API: Provides a comprehensive set of classes for date and time manipulation.
    • Example:
LocalDate today = LocalDate.now();
LocalDateTime dateTime = LocalDateTime.of(today, LocalTime.of(14, 30));
Enter fullscreen mode Exit fullscreen mode
  1. Default Methods in Interfaces
    • Interface Evolution: Allows adding methods to interfaces without breaking existing implementations.
    • Example:
public interface MyInterface {
default void defaultMethod() {
    System.out.println("Default method implementation");
    }
}
Enter fullscreen mode Exit fullscreen mode
  1. Optional Class
    • Null Handling: Encourages explicit handling of nullable values to avoid NullPointerException\.
    • Example:
Optional<String> optionalName = Optional.ofNullable(name);
optionalName.ifPresent(System.out::println);
Enter fullscreen mode Exit fullscreen mode

Java 8's features promote cleaner code, improved performance, and better support for modern programming paradigms.

Top comments (2)

Collapse
 
abhinavbharadwajr profile image
Abhinav Bharadwaj R

Hi @manishthakurani, this is a great post for people preparing for interviews.

Would be even more guiding to understand the new feature, if you incorporate the examples explaining the Before (like how things were used to be written) and After (and how it was simplified using the new features) Implementation of the logic.

Thanks once again.

Collapse
 
manishthakurani profile image
Manish Thakurani

Hi @abhinavbharadwajr thanks for your feedback, I'm working on a youtube series and an Android app for Java Interview QnA's. I'll keep you posted.