Java 8 Examples

Java 8 DoubleBinaryOperator example

Posted On
Posted By admin

In this blog post, I will be explaining how the Java 8 DoubleBinaryOperator functional interface works. To know more about functional interfaces, you can refer this blog post.

What is DoubleBinaryOperator

The DoubleBinaryOperator is a specialization of the BinaryOperator interface. To see an example of the  BinaryOperator interface, refer to this blog post.

While the BinaryOperator interface accepts any data type, the DoubleBinaryOperator interface accepts arguments of double data type.

The DoubleBinaryOperator  interface provides a method applyAsDouble. It accepts two arguments of double data type. It returns a result double data type. So it basically applies the logic in the applyAsDouble method to the input parameters and returns a double result.

Code sample for DoubleBinaryOperator

Consider the following code snippet:

DoubleBinaryOperator multiplier = (a, b) -> a * b;
double input1 = 6;
double input2 = 5;
System.out.println("Result of multiplying " + input1 + " and " + input2 + " is " + multiplier.applyAsDouble(input1, input2));

 

Here, we have written a DoubleBinaryOperator implementation that accepts two Double inputs, multiplies them and returns the result. The DoubleBinaryOperator,applyAsDouble method is implemented using a lambda expression. So when you execute this code, it will print the following output:

Result of multiplying 6.0 and 5.0 is 30.0

Conclusion

So in this article, we saw how the Java 8 DoubleBinaryOperator functional interface works. We used a lambda expression to implement it.

You can get the source code for this example along with the code for other Java 8 examples at the Github repository here.

If you'd like to watch a detailed video tutorial of this topic or other related topics, do check out my new course Learn Java 8 New Features

If you like this post, please do let me know via the comments box below.  You can also connect with me via my Facebook Page or subscribe to my Youtube channel!

Related Post

leave a Comment