Java.util.function.DoubleBinaryOperator interface with Examples

The DoubleBinaryOperator interface was introduced in Java 8. It represents an operation on two double values and returns the result as a double value. It is a functional interface and thus can be used as a lambda expression or in a method reference. It is mostly used when the operation needs to be encapsulated from the user.

Methods:

  1. applyAsDouble(): This function takes two double values, performs the required operation and returns the result as a double.

    public double applyAsDouble(double val1, double val2)
    

Example to demonstrate DoubleBinaryOperator interface as a lambda expression .

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate DoubleBinaryOperator
  
import java.util.function.DoubleBinaryOperator;
  
public class DoubleBinaryOperatorDemo {
    public static void main(String[] args)
    {
        double x = 7.654;
        double y = 5.567;
  
        // Representing addition as
        // the double binary operator
        DoubleBinaryOperator doubleBinaryOperator
            = (a, b) -> { return a + b; };
        System.out.println("x + y = "
                           + doubleBinaryOperator
                                 .applyAsDouble(x, y));
  
        // Representing subtraction as
        // the double binary operator
        doubleBinaryOperator
            = (a, b) -> { return a - b; };
        System.out.println("x - y = "
                           + doubleBinaryOperator
                                 .applyAsDouble(x, y));
  
        // Representing multiplication as
        // the double binary operator
        doubleBinaryOperator
            = (a, b) -> { return a * b; };
        System.out.println("x * y = "
                           + doubleBinaryOperator
                                 .applyAsDouble(x, y));
  
        // Representing division as
        // the double binary operator
        doubleBinaryOperator
            = (a, b) -> { return a / b; };
        System.out.println("x / y = "
                           + doubleBinaryOperator
                                 .applyAsDouble(x, y));
  
        // Representing remainder operation
        // as the double binary operator
        doubleBinaryOperator
            = (a, b) -> { return a % b; };
        System.out.println("x % y = "
                           + doubleBinaryOperator
                                 .applyAsDouble(x, y));
    }
}

chevron_right


Output:

x + y = 13.221
x - y = 2.0869999999999997
x * y = 42.609818000000004
x / y = 1.3748877312735763
x % y = 2.0869999999999997

Reference: https://docs.oracle.com/javase/8/docs/api/java/util/function/DoubleBinaryOperator.html

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.