---
title: "Understanding Java Operators"  
description: "Java operators are special symbols used to perform operations on variables and values, forming the basis of all computations and logical operations in"  
author: "Ravi Vishwakarma"  
published: 2024-06-03  
updated: 2024-06-03  
canonical: https://www.mindstick.com/blog/304299/understanding-java-operators  
category: "java"  
tags: ["java", "javac"]  
reading_time: 5 minutes  

---

# Understanding Java Operators

Operators in Java are special symbols or keywords used to perform operations on variables and values. They form the basis of all computations and logical operations in a [Java program](https://www.mindstick.com/articles/335984/explain-the-system-out-println-and-system-in). Operators in Java can be categorized into several types based on their [functionality](https://www.mindstick.com/blog/136/using-watermark-functionality-in-textbox-by-jquery):

### 1. Arithmetic Operators

[Arithmetic operators](https://www.mindstick.com/forum/157625/what-is-meant-by-arithmetic-operators-in-python) are used to perform basic mathematical operations.

| **Operator** | **Description** | **Example** |
| --- | --- | --- |
| **+** | Addition | **a + b** |
| **-** | Subtraction | **a - b** |
| ***** | Multiplication | **a * b** |
| **/** | Division | **a / b** |
| **%** | Modulus (remainder) | **a % b** |

### 2. Unary Operators

Unary operators require only one operand and perform various operations such as incrementing/decrementing a value by one, negating an expression, or inverting the value of a boolean.

| **Operator** | **Description** | **Example** |
| --- | --- | --- |
| **+** | Unary plus (positive value) | **+a** |
| **-** | Unary minus (negation) | **-a** |
| **++** | Increment | **++a** or **a++** |
| **--** | Decrement | **--a** or **a--** |
| **!** | Logical NOT | **!booleanValue** |

### 3. Assignment Operators

[Assignment](https://www.mindstick.com/articles/126300/apa-citation-style-get-to-know-about-it-for-your-next-assignment) operators are used to assign values to variables.

| **Operator** | **Description** | **Example** |
| --- | --- | --- |
| **=** | Assign | **a = b** |
| **+=** | Add and assign | **a += b** (same as **a = a + b**) |
| **-=** | Subtract and assign | **a -= b** (same as **a = a - b**) |
| ***=** | Multiply and assign | **a *= b** (same as **a = a * b**) |
| **/=** | Divide and assign | **a /= b** (same as **a = a / b**) |
| **%=** | Modulus and assign | **a %= b** (same as **a = a % b**) |

### 4. Relational Operators

Relational operators are used to [compare two](https://www.mindstick.com/forum/34476/specific-cast-is-not-valid-in-linq-query-when-compare-two-tables) values. They return a boolean result.

| **Operator** | **Description** | **Example** |
| --- | --- | --- |
| **==** | Equal to | **a == b** |
| **!=** | Not equal to | **a != b** |
| **>** | Greater than | **a > b** |
| **<** | Less than | **a < b** |
| **>=** | Greater than or equal to | **a >= b** |
| **<=** | Less than or equal to | **a <= b** |

### 5. Logical Operators

Logical operators are used to [combine multiple](https://www.mindstick.com/forum/160929/help-with-writing-a-query-to-combine-multiple-rows-into-one-in-sql-server) boolean expressions.

| **Operator** | **Description** | **Example** |
| --- | --- | --- |
| **&&** | Logical AND | **a && b** |
| ` |  | ` |
| **!** | Logical NOT | **!a** |

### 6. Bitwise Operators

[Bitwise operators](https://www.mindstick.com/forum/160333/what-are-bitwise-operators-and-their-primary-use-cases-in-javascript) are used to perform bit-level operations on integral types.

| **Operator** | **Description** | **Example** |
| --- | --- | --- |
| **&** | Bitwise AND | **a & b** |
| ` | ` | Bitwise OR |
| **^** | Bitwise XOR | **a ^ b** |
| **~** | Bitwise complement | **~a** |
| **<<** | Left shift | **a << 2** |
| **>>** | Right shift | **a >> 2** |
| **>>>** | Unsigned right shift | **a >>> 2** |

### 7. Ternary Operator

The [ternary operator](https://www.mindstick.com/forum/12944/ternary-operator-in-razor-using-a-model) is a shorthand for an **if-else** statement. It takes three operands.

| **Operator** | **Description** | **Example** |
| --- | --- | --- |
| **? :** | Ternary (conditional) | **condition ? value1 : value2** |

### 8. Instanceof Operator

The **instanceof** operator is used to test whether an object is an instance of a specific class or interface.

| **Operator** | **Description** | **Example** |
| --- | --- | --- |
| **instanceof** | Checks instance | **object instanceof ClassName** |

### Example Code

Here's an example demonstrating various operators in Java:

```java
public class OperatorsExample {
    public static void main(String[] args) {
        int a = 10, b = 5;

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

        // Unary Operators
        System.out.println("a++ = " + (a++)); // Post-increment
        System.out.println("++a = " + (++a)); // Pre-increment
        System.out.println("a-- = " + (a--)); // Post-decrement
        System.out.println("--a = " + (--a)); // Pre-decrement

        // Assignment Operators
        a += b; // Equivalent to a = a + b
        System.out.println("a += b = " + a);
        a -= b; // Equivalent to a = a - b
        System.out.println("a -= b = " + a);

        // Relational Operators
        System.out.println("a == b: " + (a == b));
        System.out.println("a != b: " + (a != b));
        System.out.println("a > b: " + (a > b));
        System.out.println("a < b: " + (a < b));

        // Logical Operators
        boolean x = true, y = false;
        System.out.println("x && y: " + (x && y));
        System.out.println("x || y: " + (x || y));
        System.out.println("!x: " + (!x));

        // Ternary Operator
        int max = (a > b) ? a : b;
        System.out.println("Max of a and b is: " + max);

        // instanceof Operator
        String str = "Hello";
        boolean result = str instanceof String;
        System.out.println("Is str an instance of String? " + result);
    }
}
```

### Summary

Java operators are used to perform various operations on variables and values. [Understanding](https://www.mindstick.com/articles/12918/cat-5e-vs-cat-6a-understanding-the-major-differences) these operators is essential for performing computations, making decisions, and controlling the flow of Java programs. Each type of operator serves a specific purpose, from arithmetic operations to logical decisions and bit-level manipulations.

## Read more -

[**Overview of Java Scanner Class**](https://www.mindstick.com/blog/304297/overview-of-java-scanner-class)

---

Original Source: https://www.mindstick.com/blog/304299/understanding-java-operators

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
