Java Operators
In Java, operators are special symbols used to perform operations on variables and values.
They form the backbone of any Java program, enabling calculations, decisions, and logical flows.
What Is an Operator in Java?
An operator is a symbol that tells the compiler to perform specific mathematical, logical, or relational operations on operands (values or variables).
Example:
int a = 10, b = 5;
int sum = a + b; // '+' is an arithmetic operator
Here,
aandbare operands+is the operatorsumis the result
Types of Operators in Java
Java provides several categories of operators:

| Category | Description | Examples |
|---|---|---|
| Arithmetic Operators | Perform mathematical calculations | +, -, *, /, % |
| Relational Operators | Compare two values | ==, !=, >, <, >=, <= |
| Logical Operators | Combine multiple conditions | &&, ` |
| Bitwise Operators | Operate on bits | &, ` |
| Assignment Operators | Assign values to variables | =, +=, -=, *=, /=, %= |
| Unary Operators | Work with a single operand | ++, --, +, -, !, ~ |
| Ternary Operator | Conditional short form of if-else | ? : |
| instanceof Operator | Tests whether an object is of a specific type | obj instanceof ClassName |
1. Arithmetic Operators
Used for basic mathematical operations.
| Operator | Description | Example | Output |
|---|---|---|---|
+ | Addition | 10 + 5 | 15 |
- | Subtraction | 10 - 5 | 5 |
* | Multiplication | 10 * 5 | 50 |
/ | Division | 10 / 3 | 3 |
% | Modulus (Remainder) | 10 % 3 | 1 |
Example:
int a = 10, b = 3;
System.out.println(a + b);
System.out.println(a / b);
System.out.println(a % b);
2. Assignment Operators
Used to assign values to variables.
They can also perform an operation while assigning.
| Operator | Example | Equivalent To |
|---|---|---|
= | a = 5; | — |
+= | a += 5; | a = a + 5; |
-= | a -= 5; | a = a - 5; |
*= | a *= 5; | a = a * 5; |
/= | a /= 5; | a = a / 5; |
%= | a %= 5; | a = a % 5; |
3. Unary Operators
Used with a single operand to perform increment, decrement, or negation.
| Operator | Description | Example | Output |
|---|---|---|---|
+ | Unary plus (no change) | +a | a |
- | Unary minus (negates value) | -a | negative of a |
++ | Increment | a++ or ++a | increases by 1 |
-- | Decrement | a-- or --a | decreases by 1 |
! | Logical NOT | !true | false |
~ | Bitwise Complement | ~5 | -6 |
Example:
int a = 5;
System.out.println(++a); // 6
System.out.println(a--); // 6, then a = 5
System.out.println(-a); // -5
4. Relational Operators
Used to compare two values, returning a boolean result (true or false).
| Operator | Description | Example | Result |
|---|---|---|---|
== | Equal to | 5 == 5 | true |
!= | Not equal to | 5 != 3 | true |
> | Greater than | 5 > 3 | true |
< | Less than | 5 < 3 | false |
>= | Greater than or equal | 5 >= 5 | true |
<= | Less than or equal | 5 <= 4 | false |
5. Logical Operators
Used to combine multiple boolean expressions.
| Operator | Description | Example | Result |
|---|---|---|---|
&& | Logical AND | (a > 5 && b < 10) | true if both true |
| ` | ` | Logical OR | |
! | Logical NOT | !(a > 5) | reverses result |
Example:
int a = 10, b = 20;
System.out.println(a > 5 && b > 15); // true
System.out.println(a < 5 || b > 15); // true
System.out.println(!(a == b)); // true
6. Bitwise Operators
Work at bit level (0s and 1s).
Used for low-level programming, encryption, and optimization.
| Operator | Description | Example | Result |
|---|---|---|---|
& | AND | 5 & 3 | 1 |
| ` | ` | OR | `5 |
^ | XOR | 5 ^ 3 | 6 |
~ | Complement | ~5 | -6 |
<< | Left Shift | 5 << 1 | 10 |
>> | Right Shift | 5 >> 1 | 2 |
>>> | Unsigned Right Shift | -5 >>> 1 | large positive value |
Example:
int a = 5, b = 3;
System.out.println(a & b); // 1
System.out.println(a | b); // 7
System.out.println(a ^ b); // 6
System.out.println(a << 1); // 10
System.out.println(a >> 1); // 2
7. Ternary Operator (Conditional Operator)
A short form of if-else.
Syntax:
variable = (condition) ? value_if_true : value_if_false;
Example:
int age = 20;
String result = (age >= 18) ? "Eligible" : "Not Eligible";
System.out.println(result);
8. instanceof Operator
Checks whether an object belongs to a particular class or subclass.
String name = "Java";
boolean check = name instanceof String;
System.out.println(check); // true
9. Operator Precedence & Associativity
Determines the order in which operators are executed in complex expressions.
| Precedence | Operator | Description |
|---|---|---|
| Highest | (), [], . | Parentheses, array access |
++, --, !, ~ | Unary | |
*, /, % | Multiplicative | |
+, - | Additive | |
<<, >>, >>> | Shift | |
<, >, <=, >=, instanceof | Relational | |
==, != | Equality | |
&, ^, ` | ` | |
&&, ` | ||
?: | Ternary | |
| Lowest | =, +=, -= etc. | Assignment |
Example:
int result = 10 + 5 * 2; // result = 20, not 30
Example Program
public class JavaOperators {
public static void main(String[] args) {
int a = 10, b = 5;
System.out.println("Arithmetic: " + (a + b));
System.out.println("Relational: " + (a > b));
System.out.println("Logical: " + (a > 0 && b > 0));
System.out.println("Bitwise AND: " + (a & b));
System.out.println("Ternary: " + ((a > b) ? "A is greater" : "B is greater"));
}
}
Output:
Arithmetic: 15
Relational: true
Logical: true
Bitwise AND: 0
Ternary: A is greater
🏆 Top 5 Java Interview Questions - Operators in Java
1. What is an operator in Java?
Answer: An operator is a symbol that tells the compiler to perform mathematical, logical, or relational operations on operands (variables or values).
Example:
int a = 10, b = 5;
int sum = a + b; // '+' is an arithmetic operator
aandb→ operands+→ operatorsum→ result
2. Name the main types of operators in Java.
Answer: Java operators can be classified into several categories:
| Type | Description | Example |
|---|---|---|
| Arithmetic | Mathematical operations | +, -, *, /, % |
| Relational | Compare values | ==, !=, >, <, >=, <= |
| Logical | Combine boolean expressions | &&, ` |
| Bitwise | Operates at bit level | &, ` |
| Assignment | Assigns values | =, +=, -=, *=, /= |
| Unary | Works with a single operand | ++, --, +, -, !, ~ |
| Ternary | Short if-else | ?: |
| instanceof | Checks object type | obj instanceof ClassName |
3. What is the difference between unary, binary, and ternary operators?
Answer:
| Operator Type | Operands | Example | Description |
|---|---|---|---|
| Unary | 1 | ++a, !true | Works on a single operand |
| Binary | 2 | a + b, a > b | Works on two operands |
| Ternary | 3 | (a > b) ? x : y | Works on three operands (conditional operator) |
4. Explain the difference between logical and bitwise operators in Java.
Answer:
| Feature | Logical Operators | Bitwise Operators |
|---|---|---|
| Operates On | Boolean values only | Integer bits (0s and 1s) |
| Examples | &&, ` | |
| Short-circuit | Yes (&&, ` | |
| Use Case | Decision-making | Low-level programming & optimization |
Example:
int a = 5, b = 3;
System.out.println(a & b); // 1 → bitwise AND
System.out.println(a > 0 && b > 0); // true → logical AND
5. What is the ternary operator and how is it used?
Answer: The ternary operator (?:) is a short form of if-else used to assign values based on a condition.
Syntax:
variable = (condition) ? value_if_true : value_if_false;
Example:
int age = 20;
String result = (age >= 18) ? "Eligible" : "Not Eligible";
System.out.println(result); // Eligible
