In this post, we will see “Arithmetic Operators in action in Java 8?” we are going to learn how to perform arithmetic operations with the help of Lambda & Functional Interfaces in Java 8.
Arithmetic Operators in action in Java 8..!!! Click To Tweet
Here, we are going to cover below points:
- What are Arithmetic Operations?
- Perform Arithmetic operation with Lambda using In-built FunctionalInterface
- Perform Arithmetic operation with Lambda using Custom built Functional Interface
Let’s begin,
1. What are Arithmetic Operations?
Java supports various arithmetic operators; +
(addition), -
(subtraction), *
(multiplication), /
(division), and %
(modulo). The below table explains the binary arithmetic operations in this programming language.
Operator | Usage | Description |
---|---|---|
+ | o1 + o2 | Adds o1 and o2 |
- | o1 - o2 | Subtracts o2 from o1 |
* | o1 * o2 | Multiplies o1 by o2 |
/ | o1 / o2 | Divides o1 by o2 |
% | o1 % o2 | Find out the remainder of division of o1 by o2 |
2. Arithmetic Operation with Lambda using In-Built FunctionalInterface
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | import java.util.function.BiFunction; /** * Java 8 program to perform arithmetic operation of two numbers using inbuilt BiFunction Functional Interface * * @author D.V */ public class Arithmetic_Operation_Java8_Function_Example { public static void main(String[] args) { //Lambda expression for addition BiFunction <Integer, Integer, Integer> funcAddObj = (i1, i2) -> i1 + i2; //Lambda expression for subtract BiFunction <Integer, Integer, Integer> funcSubtractObj = (i1, i2) -> i1 - i2; //Lambda expression for multiply BiFunction <Integer, Integer, Integer> funcMuliplyObj = (i1, i2) -> i1 * i2; //Lambda expression for division BiFunction <Integer, Integer, Integer> funcDivideObj = (i1, i2) -> i1 / i2; //Lambda expression for division BiFunction <Integer, Integer, Integer> funcModuloObj = (i1, i2) -> i1 % i2; System.out.println("Addition of 10 and 5: " + funcAddObj.apply(10, 5)); System.out.println("Subtract of 10 and 5: " + funcSubtractObj.apply(10, 5)); System.out.println("Multiply of 10 and 5: " + funcMuliplyObj.apply(10, 5)); System.out.println("Division of 10 and 5: " + funcDivideObj.apply(10, 5)); System.out.println("Modulo of 10 and 5: " + funcModuloObj.apply(10, 5)); } } |
Output:
1 2 3 4 5 | Addition of 10 and 5: 15 Subtract of 10 and 5: 5 Multiply of 10 and 5: 50 Division of 10 and 5: 2 Modulo of 10 and 5: 0 |
2. Arithmetic Operation with Lambda using Custom Built FunctionalInterface
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | /** * Java 8 program to perform arithmetic operation of two numbers using custom build functional interface * * @author D.V */ public class Arithmetic_Operation_Java8_Function_Example2 { public static void main(String[] args) { //Lambda expression for addition Arithmetic addition = (int n1, int n2) - > n1 + n2; //Lambda expression for subtract Arithmetic subtraction = (int n1, int n2) - > n1 - n2; //Lambda expression for multiply Arithmetic multiplication = (int n1, int n2) - > n1 * n2; //Lambda expression for division Arithmetic division = (int n1, int n2) - > n1 / n2; //Lambda expression for division Arithmetic modulo = (int n1, int n2) - > n1 % n2; System.out.println("Addition of 10 and 5: " + addition.operation(10, 5)); System.out.println("Subtraction of 10 and 5: " + subtraction.operation(10, 5)); System.out.println("Multiplication of 10 and 5: " + multiplication.operation(10, 5)); System.out.println("Division of 10 and 5: " + division.operation(10, 5)); System.out.println("Modulo of 10 and 5: " + modulo.operation(10, 5)); } } @FunctionalInterface interface Arithmetic { int operation(int n1, int n2); } |
Output:
1 2 3 4 5 | Addition of 10 and 5: 15 Subtraction of 10 and 5: 5 Multiplication of 10 and 5: 50 Division of 10 and 5: 2 Modulo of 10 and 5: 0 |
Arithmetic Operators in action in Java 8..!!! Click To Tweet
Do you like this Post? – then check my other helpful posts:
- Double the even / odd numbers of a specified ArrayList using Streams
- Double the numbers of specified ArrayList using Streams