What are Java 8’s new futures?

Java 8 introduced several significant features that enhanced its functionality, particularly in dealing with concurrency and concise code/enabling functional programming. Below is a list of key features introduced in Java 8.

  1. Lambda Expressions
  2. Functional Interface
  3. Default Method’s
  4. Static Method’s
  5. Predefined Functional Interfaces
    • Predicate
    • Function
    • Consumer
    • Supplier
  6. Double Colon Operator(::)
  7. Method References
  8. Constructor References
  9. Stream API
  10. Date and Time API
  11. Optional Class
  12. Nashorn JavaScript Engine
  13. forEach() Method in Iterable Interface
  14. Collection API Improvements
  15. Concurrency API Improvements
  16. Base64 Encode & Decode

We have listed all the Java 8 features above. Now, let’s describe each one in detail to gain a better understanding. By breaking down these features individually, we’ll explore their functionalities, benefits, and how they enhance Java programming.

  1. Lambda Expressions: The main objective of lambda expressions is to bring the benefits of functional programming into Java. It’s an anonymous function nameless, without return type, without modifiers, and Lambda expressions only applicable on functional interface.
Example 1 : Normal Method
public void m1(){
       System.out.println("Hello");
}

In Lambda Expressions
()->{
      System.out.println("Hello");
}

Note:  If body contains single line you can write Lambda expression like below. You should remove {} bracket.[Optional]
()->System.out.println("Hello");

Example 2 : Normal Method
public void sum(int a, int b){
      System.out.println(a+b);
}

In Lambda Expressions
(int a, int b) -> System.out.println(a+b);

Note : Compiler can guess the types automatically so you can remove types.
()->System.out.println(a+b);

Example 3 : Normal Method
public int square(int n){
      return n*n;
}

In Lambda Expressions
Note : If return keyword is specified than curly braces mandatory.
(int n)->{
     return n*n;
}

Note : Without curly braces if you want to return something then automatically you are not required to specific return keyword.
(int n)->n*n;

Note : Compiler automatically guess the return types so remove the return type.
(n)->n*n;

Note : if only one parameter is available then you are not required to use parenthesis. It is optional.
n->n*n;

Note : Within curly braces if we want to return some value compulsory. we should use return keyword in statement.

2. Functional Interface: A functional interface is an interface that contains only one abstract method. They can have only one functionality to exhibit. From Java 8 onwards. Lambda Expression can be used to represent the instance of a functional interface.

Key Points:

  • A functional interface can have any number of default methods and static methods.
  • We don’t need to use the @FunctionalInterface annotation to mark as functional interface.
  • The @FucntionalInterface annotation is a facility to avoid accidental addition of abstract methods in the functional interface.
    • Example:
    • Runnable = run()
    • Callable =call()
    • Comparable = compareTo()
    • ActionListener = actionPerformed()

Example:

@FunctionalInterface
public interface Calculator {
    // Abstract method
    int operate(int a, int b);
}

Default and Static Interface Methods:

Before Java yet, interfaces could have only public Apex methods; it was not possible to add new functionality to the existing interface without forcing all implementing classes to create an implementation of the new methods, nor was it possible to create an interface method with an implementation.  Starting with Java, eight interfaces can have a static and default method that, despite being declared in an interface, has a defined behavior.

3. Default Method’s: A default method is a method defined in an interface with the default keyword, providing a default implementation. This means that classes implementing the interface do not need to provide an implementation for this method unless they choose to override it.

Key Points:

  • Default methods allow interfaces to evolve without breaking existing implementations.
  • They provide a way to add new functionalities to interfaces while maintaining backward compatibility.
  • Default methods can be overridden by implementing classes if a specific behavior is required.

Example:

interface TestInterface {
    // Abstract method
    void square(int a);
    
    // Default method
    default void show() {
        System.out.println("Default Method Executed");
    }
}

class JavaInfotech implements TestInterface {
    // Implementation of the abstract method
    public void square(int a) {
        System.out.println(a * a);
    }

    public static void main(String[] args) {
        JavaInfotech obj = new JavaInfotech ();
        obj.square(4); // Output: 16
        obj.show();    // Output: Default Method Executed
    }
}

4. Static Method’s: In Java 8, static methods in functional interfaces are used to provide utility functions that are associated with the interface itself rather than its instances. They complement the use of lambda expressions by offering additional functionality that can be accessed without needing an instance of the interface.

Key Points:

  • Static methods belong to the interface itself, not to any specific implementation of the interface.
  • Static methods can be called on the interface itself, using the interface name, without creating an instance of the interface.

Example:

@FunctionalInterface
public interface Converter {
    int convert(String input);

    static String getDefault() {
        return "Default value";
    }
}

public class JavaInfotech{
    public static void main(String[] args) {
        // Using the static method from the interface
        System.out.println(Converter.getDefault());

        // Using a lambda expression to implement the abstract method
        Converter converter = (input) -> Integer.parseInt(input);
        System.out.println(converter.convert("123"));
    }
}

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *