Java Operators
An operator is a symbol that performs an operation on one or more values (called operands). Operators are the building blocks of any expression or calculation in Java. For example, in 5 + 3, the + is the operator and 5 and 3 are the operands.
Java provides a rich set of operators grouped by their purpose.
1. Arithmetic Operators
Arithmetic operators perform basic mathematical operations.
| Operator | Name | Example | Result |
|---|---|---|---|
| + | Addition | 10 + 3 | 13 |
| - | Subtraction | 10 - 3 | 7 |
| * | Multiplication | 10 * 3 | 30 |
| / | Division | 10 / 3 | 3 (integer division) |
| % | Modulus (Remainder) | 10 % 3 | 1 |
public class ArithmeticDemo {
public static void main(String[] args) {
int a = 10, b = 3;
System.out.println("Addition: " + (a + b)); // 13
System.out.println("Subtraction: " + (a - b)); // 7
System.out.println("Multiplication: " + (a * b)); // 30
System.out.println("Division: " + (a / b)); // 3
System.out.println("Remainder: " + (a % b)); // 1
}
}Note: When dividing two integers in Java, the result is always an integer. Any decimal part is discarded. For decimal results, use double.
2. Assignment Operators
Assignment operators store values into variables. The basic assignment operator is =. There are also shorthand compound assignment operators.
| Operator | Meaning | Equivalent To |
|---|---|---|
| = | Assign | x = 5 |
| += | Add and assign | x = x + 5 |
| -= | Subtract and assign | x = x - 5 |
| *= | Multiply and assign | x = x * 5 |
| /= | Divide and assign | x = x / 5 |
| %= | Modulus and assign | x = x % 5 |
int x = 10;
x += 5; // x is now 15
x -= 3; // x is now 12
x *= 2; // x is now 24
System.out.println("x = " + x); // Output: x = 243. Comparison (Relational) Operators
Comparison operators compare two values and return a boolean result — either true or false. These are heavily used in conditions and loops.
| Operator | Meaning | Example | Result |
|---|---|---|---|
| == | Equal to | 5 == 5 | true |
| != | Not equal to | 5 != 3 | true |
| > | Greater than | 8 > 3 | true |
| < | Less than | 3 < 8 | true |
| >= | Greater than or equal | 5 >= 5 | true |
| <= | Less than or equal | 4 <= 6 | true |
int p = 10, q = 20;
System.out.println(p == q); // false
System.out.println(p != q); // true
System.out.println(p < q); // true
System.out.println(p >= 10); // true4. Logical Operators
Logical operators combine multiple boolean conditions.
| Operator | Name | Description | Example |
|---|---|---|---|
| && | Logical AND | Returns true if both conditions are true | true && false → false |
| || | Logical OR | Returns true if at least one condition is true | true || false → true |
| ! | Logical NOT | Reverses the boolean value | !true → false |
int age = 20;
boolean hasID = true;
// Both conditions must be true
System.out.println(age >= 18 && hasID); // true
// At least one condition must be true
System.out.println(age < 18 || hasID); // true
// Reversal
System.out.println(!hasID); // false5. Increment and Decrement Operators
These operators increase or decrease a numeric value by 1. They exist in two forms: prefix (before the variable) and postfix (after the variable).
| Operator | Type | Effect |
|---|---|---|
| ++x | Prefix increment | Increments first, then uses the value |
| x++ | Postfix increment | Uses the value first, then increments |
| --x | Prefix decrement | Decrements first, then uses the value |
| x-- | Postfix decrement | Uses the value first, then decrements |
int n = 5;
System.out.println(n++); // prints 5, then n becomes 6
System.out.println(n); // prints 6
System.out.println(++n); // n becomes 7, then prints 76. Ternary Operator
The ternary operator is a shorthand for an if-else statement. It takes three parts: a condition, a value if true, and a value if false.
variable = (condition) ? valueIfTrue : valueIfFalse;int marks = 75;
String result = (marks >= 50) ? "Pass" : "Fail";
System.out.println(result); // Output: Pass7. Bitwise Operators (Overview)
Bitwise operators work on individual bits of integer values. These are more advanced and used in low-level programming tasks.
| Operator | Name | Example |
|---|---|---|
| & | Bitwise AND | 5 & 3 → 1 |
| | | Bitwise OR | 5 | 3 → 7 |
| ^ | Bitwise XOR | 5 ^ 3 → 6 |
| ~ | Bitwise NOT | ~5 → -6 |
| << | Left shift | 5 << 1 → 10 |
| >> | Right shift | 10 >> 1 → 5 |
Operator Precedence
When multiple operators appear in the same expression, Java evaluates them in a specific order called operator precedence (similar to BODMAS in mathematics).
General precedence from highest to lowest:
- Parentheses
() - Unary operators:
++,--,! - Multiplication, division, modulus:
*,/,% - Addition, subtraction:
+,- - Comparison:
<,>,<=,>= - Equality:
==,!= - Logical AND:
&& - Logical OR:
|| - Assignment:
=,+=, etc.
int result = 10 + 2 * 5; // 2*5 is evaluated first → 10 + 10 = 20
int result2 = (10 + 2) * 5; // parentheses first → 12 * 5 = 60Summary
- Java operators perform operations on values and variables.
- Arithmetic operators:
+,-,*,/,%. - Assignment operators:
=,+=,-=, etc. - Comparison operators return
trueorfalse. - Logical operators combine boolean conditions using AND, OR, NOT.
- The ternary operator is a compact form of an if-else.
- Operator precedence determines evaluation order; use parentheses to control it explicitly.
