Operators in Java

Java provides a wide variety of operators. They can be grouped as:

  1. Arithmetic Operators (++, –, +, -, *, /, %)
  2. Bitwise Operators (~, &, |, ^, <<, >>, >>>)
  3. Relational Operators (==, !=, >, >=, <, <=)
  4. Boolean Logical Operators (!, &, |, ^, ==, !=, &&, ||)
  5. Assignment Operator (= and others of form op=)
  6. Ternary operator ( ? : )
  7. instanceof operator(instanceof)

Click on operator types to know more about them in detail. For the time being we will discuss each one of them in short here:


 

1. Arithmetic Operators

They are used in mathematical expressions similar to algebra.

For example purpose, assume that two integer variables X and Y hold value 16 and 4 respectively. Note that value of both X and Y are fixed throughout the examples in this table.
OperatorOperator NameTypeExample
++Incrementunary++X returns 17
--Decrementunary--X returns 15
+Additionunary / binaryX + Y returns 20
-Subtractionunary / binaryX - Y returns 12
*MultiplicationbinaryX * Y returns 64
/DivisionbinaryX / Y returns 4
%ModulusbinaryX % Y returns 0

 

2. Bitwise Operators

Bitwise operators can be applied to integer types including char type. They are named bitwise because they act on individual bits of their operands.

For example purpose, assume three integer variables X, Y and Z hold value 45, 25 and -35 respectively.
OperatorOperator NameTypeExample
~bitwise NOTunary~X = -46
&bitwise ANDbinaryX & Y = 9
|bitwise ORbinaryX | Y = 61
^bitwise XORbinaryX ^ Y = 52
<<Left ShiftbinaryX << 2 = 180,
Z << 2 = -140
>>Right ShiftbinaryX >> 2 = 11,
Z >> 2 = -9
>>>Unsigned Right ShiftbinaryX >>> 2 = 11,
Z >>> 2 = 1073741815

Out of these ~, &, | and ^ are called bitwise logical operators. The truth table for them is given in the table below:

XYX & YX | YX ^ Y~X
000001
010111
100110
111100

 

3. Relational Operators

Relational operators all binary operators and determine the relationship between the two operands.

For example, assume X and Y are two variables with values 7 and 10 respectively.
OperatorOperator NameTypeExample
==Equal tobinaryX == Y returns false
!=Not Equal tobinaryX != Y returns true
>Greater thanbinaryX > Y returns false
>=Greater than or Equal tobinaryX >= Y returns false
<Less thanbinaryX < Y returns true
<=Less than or Equal tobinaryX <= Y returns true

 

4. Boolean Logical Operators

The boolean logical operators act only on boolean values and produce boolean results.

For example purpose, assume two boolean variables X and Y hold values true and false respectively.
OperatorOperator NameTypeExample
!logical NOTunary!X returns false
&logical ANDbinaryX & Y returns false
|logical ORbinaryX | Y returns true
^logical XOR
(exclusive OR)
binaryX^Y returns true
==Equal tobinaryX == Y returns false
!=Not equal tobinaryX != Y returns true
&&short-circuit ANDbinaryX&&Y returns false
||short-circuit ORbinaryX||Y returns true

 

5. Assignment Operator

It is used to assign a value to a variable on the left hand side of the operator. It includes assignment operator(=) and its compound form with other operators. The compound operators are of form op= where op can be any arithmatic, bitwise or boolean operators like +, >> or &.

The expression x op= y  is equivalent to x = x op y .

OperatorTypeDescription
=-x = 5, assign value 5 to variable x.
+=Arithmaticx += y same as x = x+y
-=Arithmaticx -= y same as x = x-y
*=Arithmaticx *= y same as x = x*y
/=Arithmaticx /= y same as x = x/y
%=Arithmaticx %= y same as x = x%y
&=Bitwise, booleanx &= y same as x = x&y
|=Bitwise, booleanx |= y same as x = x|y
^=Bitwise, booleanx ^= y same as x = x^y
<<=Bitwisex <<= y same as x = x << y
>>=Bitwisex >>= y same as x = x>>y
>>>=Bitwisex >>>= y same as x = x>>>y
  • There 12 assignment operators in total.
  • &=, |=, ^= are overloaded to work like both bitwise operators and boolean operators.
  • Not all operators have compound assignment forms. For example && or ||.
Chained assignment

x=y=z=5 is equivalent to x=(y=(z=5)).

Lets see how it works:

  • First it sets z=5, and returns 5 because return value of an assignment is the value being assigned,
  • This returned value(5) is then assigned to y, and the assigned value(5) is returned.
  • This chain can go on and on.

 

6. Ternary Operator

Ternary operator works like a concise form of if-else statement. Its general form is :

As the name suggests, it takes three arguments. All three are expressions.

  • expr1 must be a boolean expression i.e. it must evaluate to either true or false.
  • expr2 and expr3 must be type compatible with each-other i.e. they must evaluate to the same data type. (They need not be a boolean expression)

This is how it works:

  • If expr1 evaluates to true, expr2 is evaluated and returned.
  • Else expr3 is evaluated and returned.

The returned value may be assigned to a variable or returned from a method depending on the requirements.

For x=5 and y=2

  • In the expression z = x>y ? 100 : -100 , z evaluates to 100 because x>y is true. So ternary operator returns 100.
  • In the expression z = x<y ? 100 : -100 , z evaluates to -100 because x<y is false. So ternary operator returns -100.

 

7. instanceof operator

The instanceof operator is used to test if an object is an instance of a class. The syntax is:

If the objref is of the specified type or can be cast into the specified type, it returns true else it returns false.

For example, assume that Male is a class which is a subclass of Human which in turn is a subclass of Animal. Suppose Male_ref is an object of type Male, then followings are true:

  • Male_ref instanceof Male, because Male_ref is an object of Male
  • Male_ref instanceof Human, because Male_ref can be cast into Human as it is the parent of Male class.
  • Male_ref instanceof Animal, because Male_ref can be cast into Animal as it is the ancestor of Male class.

For examples and more details about each type of operators, visit their respective pages.