Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is the DoubleBinaryOperator functional interface in Java?

abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

DoubleBinaryOperator is a functional interface that operates on two double valued parameters/operands and produces a double valued result. The interface contains one method, applyAsDouble.

The DoubleBinaryOperator interface is defined in the java.util.function package. To import the DoubleBinaryOperator interface, use the following import statement:

import java.util.function.DoubleBinaryOperator;

The applyAsDouble method

The applyAsDouble method applies the function to the given arguments of type double and produces a result of type double. This is the functional method of the interface.

Syntax

double applyAsDouble(double left, double right)

Parameters

  • double left: The first operand.
  • double right: The second operand.

Return value

This method returns a result of type double.

Code

import java.util.function.*;
public class Main{
public static void main(String[] args) {
// Implementation of DoubleBinaryOperator interface that returns product of two operands
DoubleBinaryOperator doubleBinaryOperator = (a, b) -> a * b;
double firstOperand = 45.34;
double secondOperand = 234.432;
// calling applyAsDouble method of DoubleBinaryOperator
System.out.println(doubleBinaryOperator.applyAsDouble(firstOperand, secondOperand));
}
}
Implementation of the "DoubleBinaryOperator" functional interface.

Code explanation

In the code above:

  • In line 6, we create an implementation of the DoubleBinaryOperator interface, which accepts two double arguments/operands and returns the product of the arguments as a double value.
  • In lines 7 and 8, we declare two variables of type double.
  • Finally, in line 11, we call the applyAsDouble method on the variables that we created and print the result.

RELATED TAGS

java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring