Operators in Java

bitwise operators in java

Operators in Java

Here, in this page we will discuss about the operators in Java. Java provides quite a variety of operators which come in handy to the programmer for the manipulation of variables.

The operators in java are as follows:

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

1. Arithmetic Operators

Arithmetic operators are used in mathematical expressions in the same way that they are used in algebra, i.e, to perform the basic mathematical functions.

OperatorFunctionality
+Addition
Subtraction
*Multiplication
/Division
%Modulus

2. Relational Operators

Relational operators are used in making decisions and in loops. They are used to test or define some relation between entities

OperatorFunctionality
== (equal to)To check if two operands are equal or not, if yes, the output becomes true.
!= (not equal to)To check if two operands are equal or not, if not, the output becomes true.
> (greater than)To check if the left operand is greater than the right operand, if yes, then the output becomes true.
< (less than)To check if the left operand is less than the right operand, if yes, the output becomes true.
>= (greater than or equal to)To check if the left operand is greater than or equal to the right operand, if yes, then the output becomes true.
<= (less than or equal to)To check if the left operand is less than or equal to the right operand, if yes, the output becomes true.

3. Bitwise Operators

Since the operations in the CPU/ arithmetic unit is done in bit-level, the operators to perform bit-level operations are termed as Bitwise operators.

OperatorFunctionality
&(Bitwise AND)The result of AND is 1 only if both bits are 1.
|(Bitwise OR)The result of OR is 1 if any of the two bits is 1
^(Bitwise XOR)The result of XOR is 1 if the two bits are different.
~(Bitwise Complement)It takes one number and inverts all bits of it
<<(Shift left)Left shifts the bits of the first operand, the second operand decides the number of places to shift.
>>(Shift right)Right shifts the bits of the first operand, the second operand decides the number of places to shift.

4. Logical Operators

Logical operators are mainly used to control program flow. They allow a program to make a decision based on multiple conditions.

OperatorFunctionality
&&(Logical AND)It returns true if both statements are true
||(Logical OR)It returns true if one of the statements is true
!(Logical NOT)It reverses the result, returns false if the result is true

5. Assignment Operators

Assignment operators are used to assigning the value on its right to the variable on its left.

OperatorFunctionality
= (simple assignment)Assigns values from right side operands to left side operand.
+= (compound of  + and =)It adds right operand to the left operand and assign the result to left operand.
-=(compound of  – and =)It subtracts the right operand from the left operand and assigns the result to the left operand.
*= (compound of  * and =)It multiplies the right operand with the left operand and assigns the result to the left operand.
/= (compound of  / and =)It divides the left operand with the right operand and assigns the result to the left operand.
%= (compound of  % and =)It takes modulus using two operands and assigns the result to the left operand.