Operators in Java

Table of Contents

Operators in Java

What is an Operator in Java and how to use an operator?

The operator is something that can operate between two Operands and produce results. So, Java comes with a huge set of operators which can be segregated in the following way:

  • Arithmetic Operators
  • Relational Operators
  • Bitwise Operators
  • Logical Operators
  • Assignment Operators
  • Miscellaneous Operators

Arithmetic Operators

Arithmetic Operators are use for Mathematical operations.

OperatorExampleDescription
+a + bperforms addition
a – bperforms subtraction
*a * bperforms multiplication
/a / bperforms division(integer)
%a % bperform modulus(decimals)
++a++increment by 1
a–decrement by 1

Relational Operators

Relational operators are used for establishing the relationship between the two operands.

OperatorExampleDescription
==a==bchecks if equal
!=a!=bchecks if not equal
>a>bchecks if a is greater than b
<a<bchecks if b is greater than a
>=a>=bchecks if a is greater than or equal to b
<=a<=bchecks if a is lesser than or equal to b

Bitwise Operator

Bitwise operator works on bits and performs bit-by-bit operation.Java defines several bitwise operators, which can be applied to the integer types, long, int, short, char, and byte.

If you are familiar with binary Bitwise Operations performed in Digital Computation.

For Example,

If you add 1 and 1, then it’s 10 in binary, which is obviously the binary representation of 2 in decimal form.

OperatorExampleDescription
&1&0 gives 0Bitwise AND operation
|1|1 gives 1Bitwise OR operation
^0^1 gives 0Bitwise XOR operation
~~1 gives 0Compliment operation
>>010>>1 gives 001Right Shift operation
<<010<<1 gives 100Left operation

Logical Operators

This type of operator is used for generating results on the basis of a comparison of two operands.

OperatorExampleDescription
&&1&&0 gives FalseChecks operands using AND logic
||1||0 gives TrueChecks operands using OR logic
!1!=0 gives trueChecks operands using NOT logic

Assignment Operator

These operators are used for assigning values to a variable, which may be conditional or non-conditional.

OperatorExampleDescription
=a = 5 stores 5 to variable aAssigns values from right side operands to left side operand.
+=a += 5 adds 5 to the previously stored value of a.It adds right operand to the left operand and assign the result to left operand
-=a -= 5 subtracts 5 to the previously stored value of a.It subtracts right operand from the left operand and assign the result to left operand
*=a *= 5 multiplies 5 to the previously stored value of a.It multiplies right operand from the left operand and assign the result to left operand
/=a /= 5 divides a by 5.It divides left operand by the right operand and assign the result to left operand
%=a %= 5 performs modulus operation on a by 5It operates modulus operation to the left operand by the left operand and assign the result to left operand
<<= a <<= 10 equal to a = a << 10Operates left shift and then assignment operator
>>=a >>= 10 equal to a = a >> 10Operates right shift and then assignment operator
&=a &= 10 equal to a = a & 10Operates bitwise AND operation and then assignment operator
^=a ^= 10 equal to a = a ^ 10Operates bitwise XOR operation and then assignment operator
|=a |= 10 equal to a = a | 10Operates bitwise OR operation and then assignment operator

There are a couple of more operators used in Java, which are also called Miscellaneous Operators.

Miscellaneous Operators :

  • InstanceOf Operator

The operator checks, whether the object is of a particular type (class type or interface type) or not. The syntax of using it follows:

( Object reference variable ) instanceof  (class/interface type)
  • Ternary Operator(Conditional Operators)

This operator consists of three operands and is used to evaluate Boolean expressions. The operator decides, which value should be assigned to the variable. The syntax of it is as follows:

variable x = (expression) ? value if true : value if false

Now ,Let us have look of Precedence Table

Precedence Table

TypeOperatorsAssociativity
Postfix++, —Left to right
Unary++,- -, +, –, ~ !Right to left
Multiplicative* , /, %Left to right
Additive+ , –Left to right
Shift<< , >> , >>>Left to right
Relational< ,> , <=, >=, instanceofLeft to right
Equality== ,!=Left to right
Bitwise AND&Left to right
Bitwise XOR^Left to right
Bitwise OR|Left to right
Logical AND&&Left to right
Logical OR||Left to right
Conditional? :Right to left
Assignment= ,+=, -=, *=, /=, %=, ^= ,|=,<<= ,>>= ,>>>=Right to left

Leave a Reply