Operator is a symbol that instructs the compiler to perform a specific action. For example, a “+” operator instructs the compiler to perform addition, a “>” operator instructs the compiler to perform comparison, “=” for assignment and so on. The operators in java are classified in eight different categories. In this guide, we will mainly discuss Arithmetic operators in Java.
In any operation, there is an operator and operands. For example: In a+b, the “+” symbol is the operator and a & b are operands.
Arithmetic Operators in Java
The five arithmetic operators in Java are: + (addition), – (subtraction), * (multiplication), / (division), and % (modulus) represented in the following list:
1. + Operator Example
The addition operator, represented by symbol + is used for adding two operands. In the following example, we are adding two integer numbers using + operator.
class JavaExample { public static void main(String[] args) { //declaring integer variables int x = 100, y = 20, add; add = x+y; System.out.println("Sum of x and y is: "+add); } }
Output:
Sum of x and y is: 120
In the above example, both the variables x and y are integers, so the type of variable add
is declared int.
However if one or both variables are either float or double then the type of add
cannot be int. For example:
class JavaExample { public static void main(String[] args) { int x = 100; //y is float type so the variable 'add' is //declared as float type to hold the addition result float y = 20.5f, add; //addition of int and float. The result is a float add = x+y; System.out.println("Sum of x and y is: "+add); } }
Output:
2. – Operator Example
The -(minus) arithmetic operator returns the subtraction result of two operands. In the following example, we have two variables x and y are we are subtracting the value of y from x, the result is stored in the variable subtraction
.
class JavaExample { public static void main(String[] args) { int x = 100, y = 20, subtraction; //The value of y is subtracted from value of x and //the result is stored in a variable subtraction subtraction = x-y; System.out.println("The value of x - y is: "+subtraction); } }
Output:
The value of x - y is: 80
Note: The important point to note here is that while we are saying that we are subtracting the y from x, however the values of these variables x and y remains unchanged.
Let’s print the value of both the variables after performing an arithmetic operation to see whether is there any change.
class JavaExample { public static void main(String[] args) { int x = 100, y = 20, subtraction; subtraction = x-y; System.out.println("Subtraction result: "+subtraction); //x and y remains unchanged after any arithmetic operation System.out.println("x: "+x); System.out.println("y: "+y); } }
Output:
3. * Operator Example
class JavaExample { public static void main(String[] args) { int x = 100, y = 20, multiply; multiply = x*y; System.out.println("Multiplication of x and y is: "+multiply); } }
Output:
Multiplication of x and y is: 2000
In the above example, the variables x and y are integers, this is why the variable multiply
that holds the multiplication result is int type. However, if any of the variable is float or double then the variable, where the multiplication result is stored must not be integer. Let’s look at the following example:
class JavaExample { public static void main(String[] args) { int x = 100, multiply; float y = 5.5f; multiply = x*y; System.out.println("Multiplication of x and y is: "+multiply); } }
Output: Compilation error.
Error can be fixed by changing the type of multiply
variable. A float type variable can hold the product of int and float variables.
class JavaExample { public static void main(String[] args) { int x = 100; float y = 5.5f, multiply; multiply = x*y; System.out.println("Multiplication of x and y is: "+multiply); } }
Output:
Multiplication of x and y is: 550.0
4. / Operator Example
The / operator returns the quotient after dividing an operand by another operand. For example, ‘/’ operator would return 2, if 100 is divided by 50 (100/50 == 2), however the % modulus operator would return 0 for the same operation as it returns remainder.
class JavaExample { public static void main(String[] args) { int x = 100, y = 20, division; //The '/' arithmetic operator divides the variable x by //variable y and returns the quotient division = x/y; System.out.println("Quotient value after dividing x by y: "+division); } }
Output:
Quotient value after dividing x by y: 5
Similar to the multiplication example, if any of the variable is of float or double type then the variable, where the result is stored must have the float or double data type.
class JavaExample { public static void main(String[] args) { int x = 100; float y = .25f, division; //In this arithmetic operation, one of the variable is of float //type so the 'division' variable is declared 'float' type. division = x/y; System.out.println("Quotient value after dividing x by y: "+division); } }
Output:
Quotient value after dividing x by y: 400.0
5. % Operator Example
class JavaExample { public static void main(String[] args) { int x = 100, y = 20, mod; //The modulus operator '%' returns the remainder after division //In this case, the value 100 is perfectly divisible by value 20 //thus the modulus is 0 here as no remainder. mod = x % y; System.out.println("Remainder value after dividing x by y: "+mod); } }
Output: