Java provides a wide variety of operators. They can be grouped as:
- Arithmetic Operators (++, –, +, -, *, /, %)
- Bitwise Operators (~, &, |, ^, <<, >>, >>>)
- Relational Operators (==, !=, >, >=, <, <=)
- Boolean Logical Operators (!, &, |, ^, ==, !=, &&, ||)
- Assignment Operator (= and others of form op=)
- Ternary operator ( ? : )
- 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.
|++||Increment||unary||++X returns 17|
|--||Decrement||unary||--X returns 15|
|+||Addition||unary / binary||X + Y returns 20|
|-||Subtraction||unary / binary||X - Y returns 12|
|*||Multiplication||binary||X * Y returns 64|
|/||Division||binary||X / Y returns 4|
|%||Modulus||binary||X % 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.
|~||bitwise NOT||unary||~X = -46|
|&||bitwise AND||binary||X & Y = 9|
||||bitwise OR||binary||X | Y = 61|
|^||bitwise XOR||binary||X ^ Y = 52|
|<<||Left Shift||binary||X << 2 = 180,
Z << 2 = -140
|>>||Right Shift||binary||X >> 2 = 11,
Z >> 2 = -9
|>>>||Unsigned Right Shift||binary||X >>> 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:
|X||Y||X & Y||X | Y||X ^ Y||~X|
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.
|==||Equal to||binary||X == Y returns false|
|!=||Not Equal to||binary||X != Y returns true|
|>||Greater than||binary||X > Y returns false|
|>=||Greater than or Equal to||binary||X >= Y returns false|
|<||Less than||binary||X < Y returns true|
|<=||Less than or Equal to||binary||X <= 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.
|!||logical NOT||unary||!X returns false|
|&||logical AND||binary||X & Y returns false|
||||logical OR||binary||X | Y returns true|
|binary||X^Y returns true|
|==||Equal to||binary||X == Y returns false|
|!=||Not equal to||binary||X != Y returns true|
|&&||short-circuit AND||binary||X&&Y returns false|
|||||short-circuit OR||binary||X||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 .
|=||-||x = 5, assign value 5 to variable x.|
|+=||Arithmatic||x += y same as x = x+y|
|-=||Arithmatic||x -= y same as x = x-y|
|*=||Arithmatic||x *= y same as x = x*y|
|/=||Arithmatic||x /= y same as x = x/y|
|%=||Arithmatic||x %= y same as x = x%y|
|&=||Bitwise, boolean||x &= y same as x = x&y|
||=||Bitwise, boolean||x |= y same as x = x|y|
|^=||Bitwise, boolean||x ^= y same as x = x^y|
|<<=||Bitwise||x <<= y same as x = x << y|
|>>=||Bitwise||x >>= y same as x = x>>y|
|>>>=||Bitwise||x >>>= 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 ||.
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 :
expr1 ? expr2 : expr3
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:
objref instanceof type
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.