In this article we learn the operators in java with example and types of operators.

## Operators In Java (Java Operators)

Operators in java are used to perform operations on variables and values.

### Types of Operators In Java

- Assignment operators
- Arithmetic operators
- Comparison operators
- Logical operators
- Bitwise operators

#### 1. Assignment operators In Java

**What Is Assignment operators?**

1. Assignment operator is ‘=’ operator.

2. It assigns a given value after ‘=’ to the variable.

**Why we use Assignment operators?**

It is used to assign a given value after ‘=’ to the given variable.

**When we use Assignment operators?**

When we want to assign some value to a variable,we can use assignment operator.

**How we use Assignment operators?**

Syntax:

datatype variable_name=value;

**Assignment operators in java example**

int a=10;

(It will assign value 10 to the variable a.)

#### 2. Arithmetic operators In Java

**What Is Arithmetic operators?**

1. Arithmetic operators are the operators which performs an arithmetic operations on values.

2. We have different arithmetic operators in java as: +,-,*, /, %

(They will perform addition, subtraction, multiplication, division & modulus operations resp.)

**Why we use Arithmetic operators?**

To perform an arithmetic operations on values we can use an arithmetic operators.

**When we use Arithmetic operators?**

When we want to perform an arithmetic operations like addition, subtraction, multiplication, division, modulus on values we can use arithmetic operators.

**How we use Arithmetic operators?**

Syntax:

Addition (+): var1 + var2;

Subtraction (-): var1 – var2;

Multiplication (*): var1 * var2;

Division (/): var1 / var2;

Modulus (%): var1 % var2;

**Arithmetic operators in java example**

int c= int a + int b;

int c= int a – int b;

int c= int a * int b;

int c= int a / int b;

int c= int a % int b;

#### 3. Comparison operators In Java

**What Is Comparison operators?**

1. Equal to (==): This operator checks whether the value on the operator’s left side is equal to the value in the right side.

2. Not Equal to (!=): This operator checks whether the value on the operator’s left side is not equal to the value on the right side.

3. Greater Than (>): This operator checks whether the value on the operator’s left side is greater than the value on the right side.

4. Less Than (<): This operator checks whether the value on the operator’s left side is less than the value on the right side.

5. Less Than Equal to (<=): This operator checks whether the value on the operator’s left side is less than or equal to the value on the right side.

6. Greater Than Equal to (>=): This operator checks whether the value on the operator’s left side is greater than or equal to the value on the right side.

**Why we use Comparison operators?**

Comparison Operators are used to perform comparison operation on variables and values.

**When we use Comparison operators?**

When we want to compare variables and values that time use Comparison Operators.

**How we use Comparison operators?**

Syntax:

1. v1 == v2

2. v1 != v2

3. v1 > v2

4. v1 < v2

5. v1 <= v2

6. v1 >= v2

**Comparison operators in java example**

public static void main(String[] args) {

int x = 5;

int y = 3;

System.out.println(x == y); // returns false because 5 is not equal to 3

System.out.println(x != y); // returns true because 5 is not equal to 3

System.out.println(x > y); // returns true because 5 is greater than 3

System.out.println(x < y); // returns false because 5 is not less than 3

System.out.println(x >= y); // returns true because 5 is greater, or equal, to 3

System.out.println(x <= y); // returns false because 5 is neither less than or equal to 3

}

#### 4. Logical operators In Java

**What Is Logical operators?**

1. Logical and (&&): Returns true if both statements are true.

2. Logical or (||): Returns true if one of the statements is true.

3. Logical not (!): Reverse the result, returns false if the result is true.

**Why we use Logical operators?**

Logical operators are used to determine the logic between variables or values.

**When we use Logical operators?**

When we want to determine the logic between variables and values that time use Logical operators.

**How we use Logical operators?**

Syntax:

1. v1 && v2

2. v1 || v2

3. !(v1 && v2)

**Logical operators in java example**

1. x < 5 && x < 10

2. x < 5 || x < 4

3. !(x < 5 && x < 10)

#### 5. Bitwise operators In Java

**What Is Bitwise operators?**

1. Bitwise OR (|): This operator is a binary operator, denoted by ‘|’. It returns bit by bit OR of input values, i.e., if either of the bits is 1, it gives 1, else it shows 0.

2. Bitwise AND (&): This operator is a binary operator, denoted by ‘&.’ It returns bit by bit AND of input values, i.e., if both bits are 1, it gives 1, else it shows 0.

3. Bitwise XOR (^): This operator is a binary operator, denoted by ‘^.’ It returns bit by bit XOR of input values, i.e., if corresponding bits are different, it gives 1, else it shows 0.

4. Bitwise complement (~): This operator is a unary operator, denoted by ‘~.’ It returns the one’s complement representation of the input value, i.e., with all bits inverted, which means it makes every 0 to 1, and every 1 to 0.

**Why we use Bitwise operators?**

Bitwise operators are used to performing the manipulation of individual bits of a number. They can be used with any integral type (char, short, int, etc.). They are used when performing update and query operations of the Binary indexed trees.

**When we use Bitwise operators?**

When we want to perform the manipulation of individual bits of a number.

**How we use Bitwise operators?**

Syntax:

1. v1 & v2

2. v1 | v2

3. `a^b`

4. ~a

**Bitwise operators in java example**

public class operators {

public static void main(String[] args)

{

// Initial values

int a = 5;

int b = 7;

**// bitwise and**

// 0101 & 0111=0101 = 5

System.out.println(“a&b = ” + (a & b));

**// bitwise or**

// 0101 | 0111=0111 = 7

System.out.println(“a|b = ” + (a | b));

**// bitwise xor**

// 0101 ^ 0111=0010 = 2

System.out.println(“a^b = ” + (a ^ b));

** ****// bitwise not**

// ~00000000 00000000 00000000 00000101=11111111 11111111 11111111 11111010

// will give 1’s complement (32 bit) of 5 = -6

System.out.println(“~a = ” + ~a);

// can also be combined with

// assignment operator to provide shorthand

// assignment

// a=a&b

a &= b;

System.out.println(“a= ” + a);

}

}