Subtraction operator in java
A substract operator substract right side variable or value from the left side variable or value provided to it. It returns the result after performing operation. The result can be either negative or possible depends on order in which it used. In case of multiple operator exists in the expression, it evaluate expression from left to right.
Syntax
 1 <variable> - <variable>
 2 <value> - <value>
Example
 1 int a = 30, b = 10;
 2 int c = a - b;
 3 int d = 10 - 20;

Subtract or minus operator

Subtract or minus operator
 1 package com.java;
 2 
 3 public class Example {
 4 
 5     public static void main(String[] args) {
 6         int a = 30, b = 10;
 7 
 8         int c = a - b; // subtract lower from higher value
 9         int d = 10 - 20; // subtract higher from lower value
 10 
 11         System.out.println("The result of a - b : " + c);
 12         System.out.println("The result of 10 - 20 : " + d);
 13     }
 14 }
In the above example, a variable value right operand are subtracted from left operand. If left side operand value is bigger than right side operand value, it return positive value. If left side operand value is less than right side operand value, it returns negative value. The result is assigned to the variable that will be printed.
Output
 1 The result of a - b : 20
 2 The result of 10 - 20 : -10

Expression with multiple minus operator

Expression with multiple minus operator
 1 package com.java;
 2 
 3 public class Example {
 4 
 5     public static void main(String[] args) {
 6         int a = 30, b = 10;
 7 
 8         int c = a - b - 5;
 9         System.out.println("The result of a - b - 5 : " + c);
 10     }
 11 }
In the above example, an expression is define with multiple minus operator that evaluated from right to left. It executes a - b first and then subtract value 5 from the resulting value of (a - b). The result is assign to the variable that will b printed.
Output
 1 The result of a - b - 5 : 15

Minus operator with collection

Minus operator with collection
 1 package com.java;
 2 
 3 import java.util.Arrays;
 4 import java.util.List;
 5 
 6 public class Example {
 7 
 8     public static void main(String[] args) {
 9 
 10         int total = 250;
 11         List<Integer> list = Arrays.asList(10, 25, 35, 41, 56);
 12 
 13         System.out.println("Resulting elements are :");
 14         list.forEach(ele -> {
 15             int res = total - ele;
 16             System.out.println("A value after subtracting element :" + res);
 17         });
 18     }
 19 }
In the above example, an integer variable define with value 250 and a list of integer define with 5 elements. A collection method forEach() called by passing a consumer that subtract element value from variable total and print resulting value.
Output
 1 Resulting elements are :
 2 A value after subtracting element :240
 3 A value after subtracting element :225
 4 A value after subtracting element :215
 5 A value after subtracting element :209
 6 A value after subtracting element :194

Minus operator with Stream API

Minus operator with Stream API
 1 package com.java;
 2 
 3 import java.util.Arrays;
 4 import java.util.List;
 5 
 6 public class StreamExp {
 7 
 8     public static void main(String[] args) {
 9         int total = 250;
 10 
 11         List<Integer> list = Arrays.asList(10, 25, 35, 41, 56);
 12 
 13         System.out.println("Resulting elements are :");
 14         list.stream().map(ele -> total - ele)
 15                 .forEach(ele -> System.out.println("A value by subtracting element :" + ele));
 16     }
 17 }
In the above example, an integer variable define with initial value 250 and list of integers with 5 elements. A collection method stream() called that returns Stream object. A stream method map() called that subtract element value from total variable and returns Stream object that includes resulting value of after subtraction. A resulting elements iterated using forEach() and print.
Output
 1 Resulting elements are :
 2 A value by subtracting element :240
 3 A value by subtracting element :225
 4 A value by subtracting element :215
 5 A value by subtracting element :209
 6 A value by subtracting element :194
Privacy Policy
Terms of Service
Disclaimer
Contact us
About us