refresh java logo

Operators in Java

An Operator is a special symbols for computer which tells the computer to perform specific operation on one, two or three operands and then return a result. An operand is simply a variable on which operation is performed. For example in expression a+b, a and b are operands and + is an operator that perform addition operation. The operators are divided in three types. Unary, binary and ternary. Unary operator operates on one operand, binary on two operands and ternary operates on three operands.

Assignment Operator

This is one of the most common operator that a programmer uses in programs. Assignment operator "=" assigns the value on its right side to the operand on it's left side. This operator can also be used to assign the object references.

 int age = 18; // value 18 is assigned in variable age
 Operator op = new Operator(); // An Operator class object reference is assigned in op

Arithmetic Operators

Arithmetic operators are used to perform operations like addition, subtraction, multiplication and division. These operator performs the same operation as it perform in basic mathematics. Operator " % " is also the part of arithmetic operator which divides one operand by another operand and returns the remainder as it's result.

Table below displays the list of arithmetic operators and their usage example. Consider a and b as two operands.

Operator Example Description
+ a+b Addition operator, adds a and b (+ is also used for String concatenation)
- a-b Subtraction operator, subtracts b from a
* a*b Multiplication operator, multiply a and b
/ a/b Division operator, divides a by b
% a%b Remainder or Modulo operator, divides a by b and returns the remainder

Arithmetic operators can also be combined with assignment operator to create a compound assignment. For example a+=2; and a=a+2; both increment the value of a by 2. The expression a+=2 is interpreted as a = a+2;

Java Program of Arithmetic Operators

 class ArithmeticOperator 
  {
     public static void main (String[] args)
      {
         int result = 2 + 3;				
         System.out.println("2 + 3 = " + result);
         int result_copy = result;

         result = result - 2;
         System.out.println(result_copy + " - 2 = " + result);
         result_copy = result;

         result = result*4;
         System.out.println(result_copy + " * 4 = " + result);
         result_copy = result;

         result = result/2;
         System.out.println(result_copy + " / 2 = " + result);
         result_copy = result;

         result = result % 4;
         System.out.println(result_copy + " % 4 = " + result); 
					
         result += 2; // Equivalent to result = result+2;
         System.out.println("result = " + result);
					
         result *= 3; // Equivalent to result = result*3;
         System.out.println("result = " + result);
      }
  } 

Output:

2 + 3 = 5
5 - 2 = 3
3 * 4 = 12
12 / 2 = 6
6 % 4 = 2
result = 4
result = 12

Unary Operators

The unary operators perform operations on only one operand. These operator performs operations like incrementing/decrementing a value by one, negating an expression or value, or inverting the value of a boolean. Consider a is an integer variable and isValid is a boolean variable.

Operator Example Description
+ +a, +5 Unary plus operator: indicates positive value (By default numbers are positive)
- -a, -3 Unary minus operator: negates an expression
++ a++, ++a Increment operator: increments value of a by 1
-- a--, --a Decrement operator: decrements value of a by 1
! !isValid Logical complement operator: inverts the value of a boolean

Java Program of Unary Operators

 class UnaryOperator 
  { 
     public static void main(String[] args)
      {
         int result = +5;
         System.out.println("result = "+result);

         result--;
         System.out.println("result = "+result);

         result++;
         System.out.println("result = "+result);
						
         result = -result;
         System.out.println("result = "+result);
         
         int i = 5;
         i++;
		
         // prints 6				
         System.out.println("i = "+i);
         ++i;			   
         // prints 7
         System.out.println("i = "+i);
         // prints 8
         System.out.println("i = "+ ++i);
         // prints 8
         System.out.println("i = "+ i++);
         // prints 9
         System.out.println("i = "+i);

        boolean isValid = false;
        System.out.println("isValid = "+ !isValid);
      }
  } 

Output:

result = 5
result = 4
result = 5
result = -5
i = 6
i = 7
i = 8
i = 8
i = 9
isValid = true

The increment/decrement operator can be applied before(prefix) or after(postfix) the operand. Both operator increment/decrement the value of operand by 1. The difference between both version is that the prefix version (++result) evaluates to the incremented value, whereas the postfix version (result++) evaluates to the original value.

★★★
  • All arithmetic operators are binary operator because they are used with two operands.
  • Programmer need to be well clear while using prefix or postfix increment/decrement in larger expression.
  • Keep in mind that you must use "==", not "=", when testing if two primitive values are equal.