# Arithmetic Operators in Java Explained [Practical Examples]

## Introduction to Arithmetic Operators

In Java, arithmetic operators are binary operators that performs the mathematical operations. In other words, Java's arithmetic operators perform addition, subtraction, multiplication and division. However, subtraction operator is also used as a unary operator. These operators return a numeric value and are widely used in computation statements.
Java supports following Arithmetic operators.

• Subtraction Operator (-)
• Multiplication Operator (*)
• Division Operator (\)
• Modulo Operator (%)

The generalized syntax to use all the arithmetic operator is as shown below:

``````left_operand arithmetic_operator right_operand
``````

## Examples of different Arithmetic Operators in Java

In Java, addition operator "`+`" is used as a binary operator. We can use + operator with any numeric type as well as char type. When we use `+` with char type data, it takes the ASCII value of that char and operates on same. Moreover, when we use it with string objects, it concatenate two strings. Hence, it returns either a numeric value or a string. However, if the two operands are of different datatypes, implicit datatype promotion occurs in such a way that the value of lower datatype is promoted to the higher datatype.

The statements below demonstrate the "`+`" operator with the operands.

``````
result = left_operand + right_operand
``````

Example :

``````public class ArithmeticDemo
{
public static void main(String[] args)
{
//Initializing
float a=10.5f;
int b=20;
char c='A';
String s1 = "Hello";
String s2 = "Java";

//Using + operator with float and int number
System.out.println("Result of Addition of a="+a+" and b = "+b+" is "+(a+b));

//Using + operator with char data and int number
System.out.println("Adding numeric value "+b+" to char value "+c+" : Result is "+(b+c));

//Using + operator to concatenate two strings
System.out.println("String Concatenation "+(s1+s2));

}
}
``````

Output

``````Result of Addition of a=10.5 and b = 20 is 30.5
Adding numeric value 20 to char value A : Result is 85
String Concatenation HelloJava
``````

### 2. Subtraction Operator (-)

In Java, subtraction operator "`-`" is used as either a unary operator or a binary operator. We can use - operator with any numeric type as well as char type. When we use `-` with char type data, it takes the ASCII value of that char and operates on same. Hence, it returns a numeric value. However, if the two operands are of different datatypes, implicit datatype promotion occurs in such a way that the value of lower datatype is promoted to the higher datatype. Moreover, When `-` operator is used as a unary operator, it negates the numeric value of a variable.

The statements below demonstrate two different ways to use - operator with the operands.

``````result = left_operand - right_operand
result = -operand
``````

Example :

``````public class ArithmeticDemo
{
public static void main(String[] args)
{
//Initializing
float a=30.5f;
int b=20;
char c='A';

//Using - operator with float and int number
System.out.println("Result of Subtraction of a="+a+" and b = "+b+" is "+(a-b));

//Using - operator with char data and int number
System.out.println("Subtracting numeric value "+b+" from char value "+c+" : Result is "+(c-b));

//Using - operator to negate the numeric value
int d= -b;
System.out.println("Value of d is "+d);

}
}
``````

Output

``````Result of Subtraction of a=30.5 and b = 20 is 10.5
Subtracting numeric value 20 from char value A : Result is 45
Value of d is -20
``````

### 3. Multiplication Operator (*)

In Java, multiplication operator "`*`" is used as a binary operator. We can use `*` operator with any numeric type as well as char type. When we use `*` with char type data, it takes the ASCII value of that char and operates on same. Hence, it always returns a numeric value. However, if the two operands are of different datatypes, implicit datatype promotion occurs in such a way that the value of lower datatype is promoted to the higher datatype.

The statement below demonstrates use of "`*`" operator with the operands.

``````result = left_operand * right_operand
``````

Example :

``````public class ArithmeticDemo
{
public static void main(String[] args)
{
//Initializing
char c= 'A';
int b=10;

//Using * operator to compute area of circle

//Using - operator with char data and int number
System.out.println("Multiplying "+b+" with char value "+c+" : Result is "+(b*c));

}
}
``````

Output

``````Area of circle with radius 10.5 is 346.3602975
Multiplying 10 with char value A : Result is 650
``````

### 4. Division Operator (/)

In Java, division operator "`/`" is used as a binary operator. We can use / operator with any numeric type as well as char type. When we use `/` with char type data, it takes the ASCII value of that char and operates on same. Hence, it always returns a numeric value. However, if the two operands are of different datatypes, implicit datatype promotion occurs in such a way that the value of lower datatype is promoted to the higher datatype. When we divide a number by zero, it throws an `ArithmeticException`.

The statement below demonstrate use of "`/`" operator with the operands.

``````result = left_operand / right_operand
``````

Example :

``````public class ArithmeticDemo
{
public static void main(String[] args)
{
//Initializing
double n1=10.5;
int n2=12;
char c= 'A';
float b=10;

//Using / operator to compute average of two numbers
System.out.println("Computing average of "+n1+" and "+n2+" : Result is "+((n1+n2)/2));

//Using / operator with char data and float number
System.out.println("Dividing the char value "+c+" by float value "+b+" : Result is "+(c/b));

}
}
``````

Output

```Computing average of 10.5 and 12 : Result is 11.25
Dividing the char value A by float value 10.0 : Result is 6.5```

### 5. Modulo Operator (%)

In Java, modulo operator "`%`" is used as a binary operator. We can use `%` operator to get the remainder of the two operands. We can use it with integer or floating type variables. However, if we perform this operation with second operand as zero then it throws an ArithmeticException. While using modulo operator with signed numerals, result makes use of sign of dividend and not of divisor.

The statement below demonstrates use of % operator with the operands.

``````result = left_operand % right_operand
``````

Example :

``````public class ArithmeticDemo
{
public static void main(String[] args)
{
//Initializing
double n=10.5;
int b=-12;
int c=7;
float d=2;

//Using % operator
System.out.println("Computing "+b+" % "+c+" : Result is "+(b%c));
System.out.println("Computing "+c+" % "+b+" : Result is "+(c%b));

//Using % operator with float number
System.out.println("Computing "+n+" % "+d+" : Result is "+(n%d));

}
}
``````

Output

``````Computing -12 % 7 : Result is -5
Computing 7 % -12 : Result is 7
Computing 10.5 % 2.0 : Result is 0.5
``````

## Summary

The knowledge of Arithmetic operators is a key to start building the logic in Java. Arithmetic operators are extensively used in computations of simple mathematical operations. In this tutorial, we covered all arithmetic operators supported in Java. We learned in detail about the syntax and how we can use this operators with different data types with example. All in all, this tutorial, covers everything that you need to know in order to have a solid command over arithmetic operators in Java.

Java Operators Explained [Easy Examples]

## References

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation. For any other feedbacks or questions you can either use the comments section or contact me form. 