Java Operators:

  • An operator is used to perform an operation over one or more operands.
  • An operator is a character that represents an action, for example + is an arithmetic operator that represents addition.
  • Java operators use in within class.

 

Types of Operator in Java

  1. Basic Arithmetic Operators
  2. Assignment Operators
  3. Auto-increment and Auto-decrement Operators
  4. Logical Java Operators
  5. Comparison (relational) operators
  6. Bitwise Operators

 

  1. Basic Arithmetic Operators

Basic arithmetic operators in java are: +, -, *, /, %

+ is for addition.

–  is for subtraction.

* is for multiplication.

/  is for division.

% is for modulo

Note: Modulo operator returns remainder, for example 10 % 5 would return 0

 

Example of Arithmetic Operators:

Output:

 

 

  1. Assignment Operators

Assignments java operators in java are: =, +=, -=, *=, /=, %=

num2 = num1 would assign value of variable num1 to the variable.

num2 += num1 is equal to num2 = num2 + num1

num2 -= num1 is equal to num2 = num2 – num1

num2 *= num1 is equal to num2 = num2 * num1

num2 /= num1 is equal to num2 = num2 / num1

num2 %= num1 is equal to num2 = num2 % num1

 

Example of Assignment Operators:

Output:

 

  1. Auto-increment and Auto-decrements Operators

Auto-increment and Auto-decrement java Operators in java are: ++ and —

num ++ is equivalent to num=num+1;

num –- is equivalent to num=num-1;

 

Example of Auto-increment and Auto-decrement Operators

Output:

 

 

  1. Logical Operators

Logical operators in java are: &&, ||, !

  • Logical Operators are used with binary variables.
  • They are mainly used in conditional statements and loops for evaluating a condition.

 

  • b1 && b2 will return true if both b1 and b2 are true else it would return false.
  • b1 || b2 will return false if both b1 or b2 are false else it would return true.
  • !b1 would return the opposite of b1, that means it would be true if b1 is false and it would return false if b1 is true.

 

Example of Logical Operators

Output:

 

 

  1. Comparison(Relational) operators

Relational operators in Java: ==, !=, >, <, >=, <=

== returns true if both the left side and right side are equal

!= returns true if left side is not equal to the right side of operator.

> returns true if left side is greater than right.

< returns true if left side is less than right side.

>= returns true if left side is greater than or equal to right side.

<= returns true if left side is less than or equal to right side.

 

Example of Relational operators

Output:

 

 

  1. Bitwise Operators 

Bitwise operators in java: &, |, ^, ~, <<, >>

&          Bitwise AND

|           Bitwise OR

^          Bitwise exclusive OR

~          Bitwise unary NOT

<<       Shift left

>>       Shift right

 

Example of Bitwise operators

Output: