In this example, we will see “How to perform division of two numbers in Java 8?”. To achieve that, we are going to use BiFunction interface introduced in Java 8 as part of the functional interfaces and we will see it through several different ways.
Perform division in Java 8 using Functional Interface...!!! Click To Tweet
Example 1. Java 8 code for division of two integer numbers (hard-coded integers)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | import java.util.function.BiFunction; /** * Java 8 program for the division of two numbers * * @author D.V */ public class Calculate_Division_Numbers_Java8_Example1 { public static void main(String[] args) { System.out.println("Division of Two Numbers using BiFunction Interface - 'apply' example \n"); int number1 = 40; int number2 = 12; BiFunction <Integer, Integer, Integer> biFunctionObj = (i1, i2) - > (i1 / i2); System.out.println("Division of 2 integer values - " + number1 + " and " + number2 + " is: " + biFunctionObj.apply(number1, number2)); } } |
1 2 3 | Division of Two Numbers using BiFunction Interface - 'apply' example Division of 2 integer values - 40 and 12 is: 3 |
Example 2. Java 8 code for division of two integer numbers (scanner input)
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 | import java.util.Scanner; import java.util.function.BiFunction; /** * Java 8 program for the division of two numbers * * @author D.V */ public class Calculate_Division_Numbers_Java8_Example2 { private static Scanner scanner; public static void main(String[] args) { System.out.println("Division of Two Numbers using BiFunction Interface - 'apply' example \n"); scanner = new Scanner(System.in); System.out.println("\nEnter first number:"); int n1 = scanner.nextInt(); System.out.println("\nEnter second number:"); int n2 = scanner.nextInt(); BiFunction < Integer, Integer, Integer > biFunctionObj = (i1, i2) - > (i1 / i2); System.out.println("\nDivision of 2 integer values - " + n1 + " by " + n2 + " is: " + biFunctionObj.apply(n1, n2)); } } |
1 2 3 4 5 6 7 8 9 | Division of Two Numbers using BiFunction Interface - 'apply' example Enter first number: 52 Enter second number: 6 Division of 2 integer values - 52 by 6 is: 8 |
Example 3. Java 8 code for division of two double numbers (scanner input)
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 | import java.util.Scanner; import java.util.function.BiFunction; /** * Java 8 program for the division of two numbers * * @author D.V */ public class Calculate_Division_Numbers_Java8_Example3 { private static Scanner scanner; public static void main(String[] args) { System.out.println("Division of Two Double Numbers using BiFunction Interface - 'apply' example \n"); scanner = new Scanner(System.in); System.out.println("\nEnter first number:"); Double n1 = scanner.nextDouble(); System.out.println("\nEnter second number:"); Double n2 = scanner.nextDouble(); BiFunction < Double, Double, Double > biFunctionObj = (i1, i2) - > (i1 / i2); System.out.println("\nDivision of 2 double values - " + n1 + " by " + n2 + " is: " + biFunctionObj.apply(n1, n2)); } } |
1 2 3 4 5 6 7 8 9 | Division of Two Double Numbers using BiFunction Interface - 'apply' example Enter first number: 56 Enter second number: 9 Division of 2 double values - 56.0 by 9.0 is: 6.222222222222222 |
Do you like this Post? – then check my other helpful posts:
- Java 8 program for the multiplication of two numbers
- Java 8 program to calculate average of N numbers