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..!!! Share on X

 

Here, we are going to cover below points:

  1. What are Arithmetic Operations?
  2. Perform Arithmetic operation with Lambda using In-built FunctionalInterface
  3. 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

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:

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

/**
 * 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:

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..!!! Share on X

 

Do you like this Post? – then check my other helpful posts:

Other Useful References:

Author

  • Deepak Verma

    Deepak Verma is a Test Automation Consultant and Software development Engineer for more than 10 years. His mission is to help you become an In-demand full stack automation tester.

    He is also the founder of Techndeck, a blog and online coaching platform dedicated to helping you succeed with all the automation basics to advanced testing automation tricks.

    View all posts