Thursday, February 2, 2023

Comprehensive list of new features introduced in Java 8

 

Here is a comprehensive list of new features introduced in Java 8:

  1. Lambda Expressions
    • Example:

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5); numbers.forEach(n -> System.out.println(n));
  1. Stream API
    • Example:

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5); int sum = numbers.stream().reduce(0, (a, b) -> a + b); System.out.println("Sum: " + sum);
  1. Method References
    • Example:

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5); numbers.sort(Comparator.naturalOrder());
  1. Default and Static Methods in Interfaces
    • Example:

interface Shape { double area(); default void printArea(Shape shape) { System.out.println("Area: " + shape.area()); } }
  1. Date and Time API
    • Example:

LocalDate today = LocalDate.now(); System.out.println("Today's date: " + today);
  1. Optional Class
    • Example:

Optional<String> optional = Optional.of("value"); if (optional.isPresent()) { System.out.println("Value: " + optional.get()); }
  1. Base64 Encoding and Decoding
    • Example:

String text = "Hello, World!"; String encoded = Base64.getEncoder().encodeToString(text.getBytes()); System.out.println("Encoded: " + encoded); byte[] decoded = Base64.getDecoder().decode(encoded); System.out.println("Decoded: " + new String(decoded));
  1. Parallel Array Sorting
    • Example:

int[] numbers = {5, 4, 3, 2, 1}; Arrays.parallelSort(numbers); System.out.println(Arrays.toString(numbers));
  1. Functional Interfaces
    • Example:

Function<Integer, Integer> square = n -> n * n; System.out.println("Square: " + square.apply(5));
  1. Nashorn JavaScript Engine
  • Example:

ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn"); engine.eval("print('Hello, Nashorn!')");

These are all the new features introduced in Java 8, along with brief examples to demonstrate how they work.

No comments:

Post a Comment