Arithmetic Operators in Java

Arithmetic operators in Java are used to perform mathematical operations. The arithmetic operators in Java can operate on any built-in data type.

Syntax:- operand1 operator operand2

Operator Meaning
+ Addition
Substraction
* Multiplacation
/ Division
% Modulus
class ArithmeticOperator1{
   public static void main(String[] args) {
     System.out.println(50+20);
     System.out.println(50-20);
     System.out.println(50*20);
     System.out.println(50/20);
     System.out.println(50%20);
   }
 }

The unary minus operator has the effect of multiplying its operand by -1. Example:- 8*(-1) = (-8)

class Substraction{
   public static void main(String[] args) {
     int a=5;
     System.out.println("a: "+ a);
     System.out.println("-a: "+ -a);
     System.out.println("- -a: "+ - -a);
   }
 }

Output:-
a: 5
-a: -5
- -a: 5


Different behaviours of arithmetic addition operators in Java

Java doesn’t support operator overlaoding, but the + operator is the exception case. The only overloadded operator in Java is + only.

In Java, “+” operator act as
• Arithmetic addition operator
• String concatenation operator

When does + operator act as an arithmetic addition operator and when it acts as a string concatenation operator?

If both operands of “+” operators are numbers or characters, then it acts as an arithmetic addition operator. But, at least one of the operand is a string, then it acts as a string concatenation operator.

class Addition1{
   public static void main(String[] args) {
     
     //Addition operator
     System.out.println(9+10);
     System.out.println(3+'a');
     System.out.println('a'+'b');
     
     //String concatenation operator
     System.out.println("a"+"b");
     System.out.println("a"+9);
   }
 }

Output:-
19
100
195
ab
a9

In the first, second and third println() lines it act as an addition operator because both operands are numbers or characters. In fourth and fifth lines “+” operator act as string concatenation operator, in these case at least one operand is a string.

The string concatenation operator (+)

In Concatenation, the compiler evaluates the given expression from left to right given that the operators have the same precedence and once it encounters the string then it considers the rest of the expression as of a string (again based on the precedence order of the expression).

class Addition2{
   public static void main(String[] args) {

     System.out.println(1+2+3+4+"abcd");
     System.out.println("abcd"+1+2+3+4);

     System.out.println(1+2+3+4+"abcd"+5+6+7+8);
     System.out.println(1+2+3+4+"abcd"+(5+6+7+8));

     System.out.println(5+6+7+8+"abcd"+(1/2*3-4));
   }
 }

Output:-
10abcd
abcd1234
10abcd5678
10abcd26
26abcd-4

In the first line, 1+2+3+4 gives 10. So, 10 and abcd concatenated and displayed 10abcd on the screen. In the second line, the first string came so remaining expression act as a string. Hence it gives results abcd1234.

In the third line, 1+2+3+4 gives 10, then string “abcd” came so remaining expression also acts as a string and displayed 10abcd5678. In the fourth line, 1+2+3+4 gives 10 then string “abcd” and (5+6+7+8) came. (5+6+7+8) gives 26 so this line displayed 10abcd26.

Similarly, the last line (1/2*3-4) gives -4 because expression evaluated from left to right, * and / have the same precedence.

When using + operator inside System.out.println() make sure to use the parenthesis for arithmetic operation.

3) A simple example of the evaluation process of concatenation is given below

 class Addition3{
   public static void main(String[] args) {
     int x=9;
     int y=10;
     int z=x*y;
     System.out.println(z);
     System.out.println("The multiplication of "+x+" and "+y+" is "+z);
   }
 }

Output:-
90
The multiplication of 9 and 10 is 90

The evaluation process of the above statement

("The multiplication of "+x+" and "+y+" is "+z);
("The multiplication of "+9+" and "+10+" is "+90);
("The multiplication of "+"9"+" and "+10+" is "+90);
("The multiplication of 9"+" and "+10+" is "+90);
("The multiplication of 9 and "+10+" is "+90);
("The multiplication of 9 and "+"10"+" is "+90);
("The multiplication of 9 and 10"+" is "+90);
("The multiplication of 9 and 10 is "+90);
("The multiplication of 9 and 10 is "+"90");
("The multiplication of 9 and 10 is 90");

Exercise on string concatenation operator in Java

Question) Find out the output of the below programs? The programs are based on arithmetic operators in Java.

Program1

class Addition4{
   public static void main(String[] args) {
     System.out.println(10+20);
     System.out.println(10+'a');
     System.out.println('a'+'b');
     System.out.println("a"+"b");
     System.out.println("a"+10);
     System.out.println("a"+true);
     System.out.println("a"+null);
   }
 }

 

Output:-
30
107
195
ab
a10
atrue
anull

 


Program2

class Addition5{
   public static void main(String[] args) {
     System.out.println(""+10+20);
     System.out.println(10+""+20);
     System.out.println(10+20+"");
     System.out.println(""+(10+20));
   }
 }

Output:-
1020
1020
30
30


Program3

class Addition6{
   public static void main(String[] args) {
     int a=10;
     int b=20;
     System.out.println(a+b);
     System.out.println("a+b");

     System.out.println("a+b:"+a+b);
     System.out.println("a+b:"+(a+b));
     System.out.println();

     //System.out.println("a-b:"+a-b);
     //error: bad operand types for binary operator '-'

     System.out.println("a-b:"+(a-b));
     System.out.println();
     System.out.println("a*b:"+a*b);
   }
 }

Output:-
30
a+b
a+b: 1020
a+b: 30

a-b: -10

a*b: 200


Java Division Operator

20 / 2 = 10
Here,
20 is called divided
2 is called the divisor
10 is called the quotient

1) The division operator (/) produces the quotient.

class DivisionOperator1{
   public static void main(String[] args) {
     System.out.println(1/2);
     System.out.println(1F/2);
     System.out.println(1D/2);
     System.out.println(11/4);
     System.out.println(11F/4);
     System.out.println(11D/4);
   }
 }

Output:-
0
0.5
0.5
2
2.75
2.75

class DivisionOperator2{
   public static void main(String[] args) {
     System.out.println(22/7*10*10);
     System.out.println(22F/7*10*10);
     System.out.println(22D/7*10*10);
   }
 }

Output:-
300
314.2857
314.2857142857143

1) If exactly any one of the operand is negative then the result is negative. If both operands are -ve/+ve then the result is positive.

class DivisionOperator3{
   public static void main(String[] args) {
     System.out.println(9/5);
     System.out.println(9/-5);
     System.out.println(-9/5);
     System.out.println(-9/-5);
   }
 }

Output:-
1
-1
-1
1

3) When any integer number is (Note:- not a floating-point number) divided by ten then it removes the last digit of the number. Later we will write many Java basic programs using this concept.

class DivisionOperator4{
   public static void main(String[] args) {
     System.out.println(123/10);
     System.out.println(12300/10);
     System.out.println(123015/10);
   }
 }

Output:-
12
1230
12301

class DivisionOperator5{
   public static void main(String[] args) {
     System.out.println(12.3/10);
     System.out.println(1230.0/10);
     System.out.println(123.015/10);
   }
 }

Output:-
1.23
123.0
12.3015


Different cases when the Divisor is zero

Note:- In these cases dividend should not be zero.

Operation Result
Integer/0Runtime error
Integer/0.0Infinity
Floating-point/0infinity
Floating-point/0.0Infinity

We can’t divide an integer number by integer zero, it leads to runtime error: java.lang.ArithmeticException.

class DivisionOperator6{
   public static void main(String[] args) {
     System.out.println(10/0);
   }
 }
Exception in thread "main" java.lang.ArithmeticException: / by zero
	at Division1.main(A.java:3)

We can divide an integer number by floating-point zero. Its output is infinity.

class DivisionOperator7 {
     public static void main(String[] args) {
         System.out.println(10/0.0);
         System.out.println(10/-0.0);
     }
 }

Output:-
Infinity
-Infinity

We can divide a floating-point number by ZERO (both integer and floating-point), its output is infinity.

 class DivisionOperator8 {
   public static void main(String[] args) {
     System.out.println(10.0/0);
     System.out.println(10.0F/0);
     System.out.println(-10.0/0);
   }
 }

Output:-
Infinity
Infinity
-Infinity

class DivisionOperator9 {
   public static void main(String[] args) {
     System.out.println(10.0/0.0);
     System.out.println(10.0F/0.0);
     System.out.println(-10.0/0.0);
   }
 }

Output:-
Infinity
Infinity
-Infinity

Why integer/0 gives exception but in other cases the result is infinity?

From basic mathematics, we know that any number divided by zero is infinity. In the case of integral arithmetic (byte, short, int, long), there is no way to represent infinity. Hence, the integer number divided by zero gives runtime error: java.lang.ArithmeticException

When it comes to floating-point arithmetic (float and double) there it is represented as “Infinity”. Hence in getting results as Infinity or -Infinity.

> javap java.lang.Double

By using this command you can observe that there are two constant (final variable) POSITIVE_INFINITY and NEGATIVE_INFINITY are defined in Double class. These two constants are also defined in the Float class.

> javap java.lang.Integer

But those constants are not defined in Byte, Short, Integer, and Long classes.


Different cases when both dividend and divisor are zero

OperationResult
0/0java.lang.ArithmetricException
0/0.0NaN
0.0/0NaN
0.0/0.0NaN

We can’t divide an integer zero by an integer zero. It also leads to runtime error: java.lang.ArithmeticException

class DivisionOperator10{
   public static void main(String[] args) {
     System.out.println(0/0);
   }
 }
Exception in thread "main" java.lang.ArithmeticException: / by zero
	at Division2.main(A.java:3)

In remaining cases the output is NaN. “NaN” stands for “not a number”. If integer number divided by floating-point zero then we get the result as “not a number”.

class DivisionOperator11 {
     public static void main(String[] args) {
         System.out.println(0/0.0);
         System.out.println(0/-0.0);
     }
 }

Output:-
NaN
NaN

We can divide a floating-point ZERO by ZERO its output is NaN.

class DivisionOperator10{
   public static void main(String[] args) {
     System.out.println(0.0/0);
     System.out.println(-0.0/0);
     System.out.println(0.0/0);
     System.out.println(0.0/0);
   }
 }

Output:-
NaN
NaN
NaN
NaN

class DivisionOperator10{
   public static void main(String[] args) {
     System.out.println(0.0/0.0);
     System.out.println(-0.0/0.0);
     System.out.println(0.0/0.0);
     System.out.println(0.0/0.0);
   }
 }

Output:-
NaN
NaN
NaN
NaN

Why operation 0/0 gives exception but in all other cases we get result NaN?

In regular mathematics ZERO (integral or floating-point) divided by ZERO (integral or floating-point) gives an undefined result. In integral arithmetic (byte, short, int, and long) there is no way to represent the undefined result. Hence, 0/0 gives an exception.

But in the case of floating-point arithmetic than to represent undefined result Float and Double class contains NaN constant. Hence, in this case, the result is NaN.

By using javap tool you can verify that Float and Double class contain NaN constant, but Byte, Short, Integer, and Long classes don’t contain NaN constant.

> javap java.lang.Double
> javap java.lang.Integer

Arithmetic operators Modulus in Java

The remainder operator or modulus operator (%) produces the remainder after integer division i.e. x%y = x – (x/y)*y.

1) On using modulus operator (%) the sign of the remainder is always the same as the sign of the numerator.

class Modulus1{
   public static void main(String[] args) {
     System.out.println(9%5);
     System.out.println(-9%5);
     System.out.println(9%-5);
     System.out.println(-9%-5);
   }
 }

Output:-
4
-4
4
-4

2) The modulus operator (%) also operate on floats and doubles. (In C language modulus operator (%) does not operate on the Floating point number, to find remainder on a floating-point number we need fmod() function).

class Modulus2{
   public static void main(String[] args) {
     System.out.println(9.0%5);
     System.out.println(9F%5);
     System.out.println(9%5F);
     System.out.println(9D%5D);
     System.out.println(9D%5F);
   }
 }

Output:-
4.0
4.0
4.0
4.0
4.0

3) The divisor should be a non-zero value. If the dividend is an integer then it leads to runtime error: java.lang.ArithmeticException, but if the divided is a floating-point number then it gives result NaN (Not a Number).

class Modulus3{
   public static void main(String[] args) {
     System.out.println(9%0);
   }
 }
Exception in thread "main" java.lang.ArithmeticException: / by zero
	at Modulus1.main(A.java:3)
 class Modulus4{
   public static void main(String[] args) {
         System.out.println(9.5%0);
   }
 }

Output:-
NaN

4) Any integer number%10 (not a floating-point number) gives the last digits of the number. In Java, whenever you need to find a digit of the integer number then use % operator.

class Modulus5{
   public static void main(String[] args) {
     System.out.println(123%10);
     System.out.println(97%10);
   }
 }

Output:-
3
7

class Modulus6{
   public static void main(String[] args) {
     System.out.println(123.23%10);
     System.out.println(97.59784%10);
     System.out.println(123.5%10);
     System.out.println(9F%10);
     System.out.println(9D%5D);
   }
 }

Output:-
3.230000000000004
7.597840000000005
3.5
9.0
4.0


Important points on arithmetic operators in Java

Only + operator operates on a string data type. If we use any other arithmetic operators except + then we get a compile-time error.

class ArithmeticOperator2{
   public static void main(String[] args) {
     String s1 = "a";
     String s2 = "b";
     String s3 = s1 + s2;
     System.out.println(s3);
     /*
     //error: bad operand types for binary operator
     String s4 = s1 - s2;
     String s5 = s1 * s2;
     String s6 = s1 / s2;
     String s7 = s1 % s2;
     */
   }
 }

Output:-
ab

We can’t use any arithmetic operators on the boolean data type.

class ArithmeticOperator3{
   public static void main(String[] args) {
     /*
     //error: bad operand types for binary operator
     boolean bo1 = true;
     boolean bo2 = false;
     boolean bo3 = bo1 + bo2;
     boolean bo4 = bo1 - bo2;
     boolean bo5 = bo1 * bo2;
     boolean bo6 = bo1 / bo2;
     boolean bo7 = bo1 % bo2;
     */
   }
 }

ArithmeticException

While using arithmetic operators many times we get ArithmeticException. So, we should have knowledge that what is it, and when it occurs.

1) ArithmeticException is a runtime exception. It means ArithmeticException will never occur during compilation time.
2) It occurs only for Integral arithmetic (byte, short, int, long), but not for Floating-point arithmetic (float and double).
3) The division (/) and modulus (%) are only arithmetic operators which can cause ArithmeticException. The remaining other arithmetic operators will give ArithmeticException

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Reply