Operators in Java

Operators are used to perform operations on data or variables.

On the basis of number of operands, there are two types of operators:

  1. Unary Operators - works on one operand, e.g. a++.
  2. Binary Operators - works on two operands, e.g. a+b.

Note: You can add constant values to a variable.

Note: For applying operation on variables, their data types must be compatible for that operation. For example, if you try to add number in a string, it will give you an error.

Types of Operators

  1. Arithmetic Operators
  2. Assignment Operators
  3. Comparison Operators
  4. Logical Operators
  5. Bitwise Operators

We are going to discuss about these operators in detail.

Arithmetic Operators

They are used to do Arithmetic Operations on data.

OperatorOperatorDescriptionCode
+AdditionAdds two data values.int a = 10 + 20;
-SubtractionSubtracts two data values.int a = 100 - 20;
*MultiplicationMultiply two data values.int a = 3 * 2;
/DivisionDivides two data values.int a = 10 / 5;
%ModulusReturn remainder when two data values are divided.int a = 23 % 5;
++IncrementIncrement value of a variable by 1.a++;
--DecrementDecrement value of a variable by 1.a--;
  • Post-increment - it adds value at the end of instruction
  • Pre-increment - it adds value at the start of instruction.

Assignment Operators

They are used to assign values to variables. int a =10;

OperatorDescriptionCode
=Assigning 10 to int type variable a.int a = 10;
+=It is short form of "a=a+10".a+=10;
-=It is short form of "a=a-10".a-=10;
*=It is short form of "a=a*10".a*=10;
/=It is short form of "a=a/10".a/=10;
%=It is short form of "a=a%10".a%=10;

Comparison Operators

They are used to compare values and make decisions. They are most widely used in conditional statements. They return either true or false (Boolean), on the basis of which we proceed further.

For example, if 10 is greater than 5 then add a and b otherwise subtract a and b.

OperatorNameDescriptionCode
==Double equalChecks if a is equal to b.a == b;
!=Not equalChecks if a is not equal to b.a != b;
a < bLess thanChecks if a is less than b.a < b
a <= bLess than or equalChecks if a is less than or equal b.a <= b
a > bGreater thanChecks if a is greater than b.a > b
a >= bGreater than or equalChecks if a is greater than or equal b.a >= b

Logical Operators

Like comparison operators, they also return true (1) or false (0).

There are three types of logical operators:

OperatorNameCodeDescription
&&AND(a > b) && (b > c)It says that both conditions should be true. a should be greater than b and b should be greater than c.
||OR(a > b) || (b > c)It says that only one conditions should be true. a should be greater than b or b should be greater than c.
!NOT!(a > b)Inverts the result. If a is greater than b, it will return false.

Bitwise Operators

Bitwise operators perform operation on bit level. We deal with decimal values with base 10, but for using bitwise operators, computer convert base 10 to base 2 (binary format) and perform operations on bit and again convert then to base 10 and show us the output in decimal form.

OperatorNameCodeDescription
&bitwise ANDa & bBinary of 2 is 10 and of 3 is 11 and their and will be 10 in binary and computer gives the answer 2.
|bitwise ORa | bBy taking bitwise OR of 10 and 11 we will get 11 in binary and output will be 3.
~bitwise NOT~a (uniary operator)Binary of a (a=2) is 10 and bitwise not will be 01 and output will be 1.
>>Shift Righta >> 2int result = 8 << 2; // result = 2
<<Shift Lefta << 2int result = 8 >> 2; // result = 32

Operator Precedence

If we have multiple operators in an expression, then compiler will have to decide which operator will be evaluated first and which will be evaluated in the last.

The order in which the arithmetic expression is evaluated is called the order of precedence. It is also known as hierarchy of operation.

When an arithmetic expression is evaluated, the computer performs only one operation at one time. In an expression in C++ the operations are performed in the following order:

  1. All multiplications and divisions are performed first from left to right.
  2. All additions and subtractions are then performed from left to right.
  3. If the parenthesis are used in an expression, the expression within parenthesis are first computed from left to right.
  4. When parenthesis are used within parenthesis, the expression within innermost parenthesis is evaluated first.

Example

(4-(3*5))+2

  1. (3*5) is computed and returns value of 15.
  2. 4-15 is computed and then return a value of -11.
  3. -11+2 is computed and returns value of -9.

Practice Exercises

Complete these exercises to reinforce your learning and earn XP

Sign in to track your progress and earn XP!
Exercise 1 of 2Medium

What will be the output of this code?

15 XP~4 min
int a = 10, b = 3;
int result = a / b;
System.out.print("%d", result);
Exercise 2 of 2Easy

What does the modulo operator (%) return?

10 XP~2 min