Java operators specify operations to be performed on operands. In Java operators are special symbols designated so by the programming language itself. In Java it is not possible to declare your own operators. Neither can you override the behavior of existing operators. An operator may be unary, binary or ternay based on the number of operands associated with it. An operator may be both unary and binary. Depending on the contex, it may act as unary or as binary.
Types of Java Operators
Java supports the following types of operators –
- Arithmetic operators
- Assignment operators
- Unary operators
- Equality operators
- Relational operators
- Logical operators
- Bitwise operators
- Ternary operator
- instanceof operator (The type comparison operator)
- The ternary operator ( ? : )
Arithmetic Operators
Arithmetic operators allow you to perform basic math on the integers and floats. Java has the following arithmetic operators
Operator | Description |
---|---|
+ | Performs addition |
– | Performs subtraction |
* | Performs multiplication |
/ | Performs division |
% | Modulus – Divides left hand operand by right hand operand and returns the remainder. |
Unary Operators
These operators have only one operand. The operand may appear on the right hand side or to the left. These are the unary operators –
Operator | Description |
---|---|
+ | Indicates that number on its right hands side is positive |
– | Indicates that number on its right hands side is negative |
++ | Increment operator. Can be used in postfix and prefix fashion. |
— | Decrement operator. Can be used either in prefix or postfix fashion. |
! | Boolean not. Inverts the value of boolean |
An increment or decrement operator is said to be prefix if operand follows the operator. For example ++i. Here, the value of i is incremented first and then used. The converse of this is postfix where the operand comes before the operator. For example i++. In postfix usage the value of the variable i is used in an expression and thereafter incremented.
Assignment Operators
The simplest assignment operator in Java is = . Sample usage is a = b. Here the value of right hand side operand (b) is assigned to the left hand side operand (a). Java supports the following assignment operators as well –
Operator | Description |
---|---|
= | The basic assignment operator |
+= | Add and assign operator. It adds the current value of lhs operand with rhs operand and stores the result in lhs operand. |
-= | Subtract and assign operator. It subtracts the current value of rhs operand from lhs operand and stores the result in lhs operand. |
*= | Multiply and assign operator. It multiplies the current value of lhs operand with rhs operand and stores the result in lhs operand. |
/= | Divide and assign operator. It divides the current value of lhs operand with rhs operand and stores the result in lhs operand. For example x /= y; is same as x = x/y; |
%= | Modulo and assign operator. Takes the modulo of current lhs and rhs and the remaineder is assigned to left hand side operand. x %= y is same as x = x % y |
<<= | Left shift and assign. Shifts left operand by right operand and assigns the value to left operand. x <<= y is same as x = x << y. |
>>= | Right shift and assign. Shifts left operand by right operand and assigns the value to left operand. x >>= y is same as x = x >> y. |
&= | Bitwise AND and assign. ANDs left operand and right operand and assigns the value to left operand. x &= y is same as x = x & y. |
|= | Bitwise OR and assign. ORs left operand and right operand and assigns the value to left operand. x != y is same as x = x ! y. |
^= | Bitwise XOR and assign. XORs left operand and right operand and assigns the value to left operand. x ^= y is same as x = x ^ y. |
Equality and Relational Operators
These operators perform some sort of comparison between the left and right hands side operands.
Operator | Description |
---|---|
“==” | Equality check |
!= | Check if rhs and lhs operands are unequal |
> | Greater than |
< | Smaller than |
>= | Greater than or equal to |
<= | Smaller than or equal to |
Logical Operators
Java supports the following logical operators –
Logical Operator | Descrition |
---|---|
&& | Logical AND. Evaluates to true if both operands are true. Is a short circuit operator. If the first condition is found to be false, the other is never evaluated. |
|| | Logical OR. Evaluates to true if either of operands is true. Is a short circuit operator. If the first condition is found to be true, the other is never evaluated. |
! | Logical NOT. Reverses the boolean value stored in a variable |
Bitwise Operators
Java supports the following operators for bit manipulation –
Bitwise Operator | Description |
---|---|
& | Performs bitwise AND of lhs and rhs operands. The corresponding bit of result is 1 if both lhs and rhs have their corresponding bit set. |
| | Performs bitwise OR of lhs and rhs operands. The corresponding bit of result is 1 if either of lhs and rhs have their corresponding bit set. |
^ | Performs bitwise XOR of lhs and rhs operands. The corresponding bit of result is 1 if both lhs and rhs have their corresponding bit as 0. The result bit is 0 if both operands’ bit is 1. |
~ | Bitwise NOT or complement. Is unary operator. Flips all the bits in operand. |
<< | Binary left shift operator. Left operand has its bits shifted to left by an amount equal to rhs. |
>> | Binary right shift operator. Left operand has its bits shifted to right by an amount equal to rhs. |
>>> | Right shift and zeo fill. Left operand has its bits shifted to right by an amount equal to rhs and the bits moved are all replaced by 0 bits. |
instanceof Operator
This operator is used to perform object type checking at runtime. The general format of the operator is
(Reference Type Variable) instanceof ( Class/Interface)
The operator returns a boolean value. A true value is returned if lhs operand IS-A type of rhs operand.
Ternary Operator
Also known as the conditional operator, the ternary operator is the only operator that has three operands. The ternary operator is a kind of assignment operator. This operator assigns a value to the leftmost operand based on a condition. If the condition is true a particular value is assigned and if the value is false, another value is assigned.
The general form of this operator is
leftmostOperand = (expression) ? value1 : value2
If expression evaluates to true then leftmostOperand is assigned a value value1 and value2 otherwise.
For example
1234567 | int taxRate; boolean arePoliticiansHonest = false ; taxRate = (arePoliticiansHonest) ? 5 : 105 ; System.out.println( "Your tax rate is " + taxRate); |
In the above code, tax rate is calculated based on the condition. Since the condition evaluates to false, the second value specified after the ‘:’ is assigned to the variable taxRate.