# Operators in Java

## Introduction

Java is one of the most popular programming languages used for developing web applications, enterprise applications, mobile applications, and backend systems.

In Java programming, **operators** are special symbols that are used to perform operations on variables and values. Operators help us perform calculations, compare values, assign values, and make logical decisions.

For example:

*   `+` operator is used for addition
    
*   `==` operator is used for comparison
    
*   `&&` operator is used for logical operations
    

Understanding operators is one of the fundamental concepts in Java because they are used in almost every Java program.

* * *

# Types of Operators in Java

Java operators are classified into the following categories:

1.  Arithmetic Operators
    
2.  Assignment Operators
    
3.  Relational Operators
    
4.  Logical Operators
    
5.  Unary Operators
    
6.  Bitwise Operators
    
7.  Ternary Operator
    
8.  Shift Operators
    

* * *

# 1\. Arithmetic Operators in Java

Arithmetic operators are used to perform mathematical operations.

### Arithmetic Operators

| Operator | Description |
| --- | --- |
| + | Addition |
| \- | Subtraction |
| \* | Multiplication |
| / | Division |
| % | Modulus |

### Example

```java
public class ArithmeticExample {

    public static void main(String[] args) {

        int a = 20;
        int b = 10;

        System.out.println("Addition: " + (a + b));
        System.out.println("Subtraction: " + (a - b));
        System.out.println("Multiplication: " + (a * b));
        System.out.println("Division: " + (a / b));
        System.out.println("Modulus: " + (a % b));

    }
}
```

### Output

```plaintext
Addition: 30
Subtraction: 10
Multiplication: 200
Division: 2
Modulus: 0
```

* * *

# 2\. Assignment Operators in Java

Assignment operators are used to assign values to variables.

### Assignment Operators

| Operator | Example |
| --- | --- |
| \= | a = 10 |
| += | a += 5 |
| \-= | a -= 5 |
| \*= | a \*= 5 |
| /= | a /= 5 |

### Example

```java
public class AssignmentExample {

    public static void main(String[] args) {

        int number = 10;

        number += 5;

        System.out.println(number);

    }
}
```

### Output

```plaintext
15
```

Explanation:

```java
number += 5;
```

is equal to:

```java
number = number + 5;
```

* * *

# 3\. Relational Operators in Java

Relational operators are used to compare two values.

The result will always be either `true` or `false`.

### Relational Operators

| Operator | Description |
| --- | --- |
| \== | Equal to |
| != | Not equal |
| \> | Greater than |
| < | Less than |
| \>= | Greater than or equal |
| <= | Less than or equal |

### Example

```java
public class RelationalExample {

    public static void main(String[] args) {

        int age = 20;

        System.out.println(age >= 18);

        System.out.println(age == 25);

    }
}
```

### Output

```plaintext
true
false
```

* * *

# 4\. Logical Operators in Java

Logical operators are used to combine multiple conditions.

### Logical Operators

| Operator | Description |
| --- | --- |
| && | Logical AND |
| || | Logical OR |
| ! | Logical NOT |

### Example

```java
public class LogicalExample {

    public static void main(String[] args) {

        int age = 25;
        boolean hasID = true;

        System.out.println(age >= 18 && hasID);

    }
}
```

### Output

```plaintext
true
```

Explanation:

The `&&` operator returns true only when both conditions are true.

* * *

# 5\. Unary Operators in Java

Unary operators work with only one operand.

### Unary Operators

| Operator | Description |
| --- | --- |
| ++ | Increment |
| \-- | Decrement |
| + | Positive |
| \- | Negative |
| ! | Reverse condition |

### Example

```java
public class UnaryExample {

    public static void main(String[] args) {

        int count = 5;

        count++;

        System.out.println(count);

    }
}
```

### Output

```plaintext
6
```

* * *

# 6\. Bitwise Operators in Java

Bitwise operators perform operations on binary values.

### Bitwise Operators

| Operator | Description |
| --- | --- |
| & | Bitwise AND |
| | | Bitwise OR |
| ^ | Bitwise XOR |
| ~ | Bitwise Complement |

### Example

```java
public class BitwiseExample {

    public static void main(String[] args) {

        int a = 5;
        int b = 3;

        System.out.println(a & b);

    }
}
```

### Output

```plaintext
1
```

Explanation:

Binary representation:

```plaintext
5 = 101
3 = 011

AND

101
011
---
001
```

Result:

```plaintext
1
```

* * *

# 7\. Ternary Operator in Java

The ternary operator is a shortcut for the if-else statement.

### Syntax

```java
condition ? value1 : value2;
```

### Example

```java
public class TernaryExample {

    public static void main(String[] args) {

        int age = 20;

        String result = (age >= 18) ? "Adult" : "Minor";

        System.out.println(result);

    }
}
```

### Output

```plaintext
Adult
```

* * *

# 8\. Shift Operators in Java

Shift operators are used to shift bits left or right.

### Shift Operators

| Operator | Description |
| --- | --- |
| << | Left shift |
| \>> | Right shift |
| \>>> | Unsigned right shift |

### Example

```java
public class ShiftExample {

    public static void main(String[] args) {

        int number = 8;

        System.out.println(number << 2);

    }
}
```

### Output

```plaintext
32
```

Explanation:

```plaintext
8 = 1000

After left shift by 2:

100000 = 32
```

* * *

# Operator Precedence in Java

When multiple operators are used in a single expression, Java follows operator precedence rules.

Example:

```java
public class PrecedenceExample {

    public static void main(String[] args) {

        int result = 10 + 5 * 2;

        System.out.println(result);

    }
}
```

### Output

```plaintext
20
```

Explanation:

Multiplication has higher priority than addition.

Execution:

```plaintext
5 * 2 = 10

10 + 10 = 20
```

* * *

# Real-Time Example Using Operators

Example: Employee Eligibility Check

```java
public class EligibilityCheck {

    public static void main(String[] args) {

        int age = 22;
        boolean graduation = true;

        if(age >= 18 && graduation) {

            System.out.println("Eligible");

        } else {

            System.out.println("Not Eligible");

        }

    }
}
```

### Output

```plaintext
Eligible
```

Used operators:

*   Relational operator → `>=`
    
*   Logical operator → `&&`
    

* * *

# Conclusion

Operators are one of the important building blocks of Java programming.

They are used for:

*   Performing calculations
    
*   Comparing values
    
*   Making logical decisions
    
*   Controlling program execution
    
*   Working with binary data
    

A strong understanding of operators helps developers write efficient and logical Java programs.

* * *
