Operators in Java are special symbols that perform specific operations on one, two, or three operands, and then return a result. Java provides a rich set of operators to manipulate variables and perform computations. Operators are the foundation of any programming language and are essential for building logic in your programs.
Java operators are classified into several categories: arithmetic, relational, logical, bitwise, assignment, unary, ternary, and special operators like instanceof. Understanding these operators and their precedence is crucial for writing efficient and bug-free code.
Arithmetic operators are used to perform basic mathematical operations like addition, subtraction, multiplication, division, and modulus.
| Operator | Name | Example | Result | Description |
|---|---|---|---|---|
| + | Addition | 5 + 3 | 8 | Adds two operands |
| - | Subtraction | 5 - 3 | 2 | Subtracts second operand from first |
| * | Multiplication | 5 * 3 | 15 | Multiplies two operands |
| / | Division | 10 / 3 | 3 | Divides first operand by second (integer division) |
| % | Modulus | 10 % 3 | 1 | Returns remainder of division |
| ++ | Increment | a++ | a = a + 1 | Increases value by 1 |
| -- | Decrement | a-- | a = a - 1 | Decreases value by 1 |
public class ArithmeticOps {
public static void main(String[] args) {
int a = 10, b = 3;
// Basic arithmetic
System.out.println("a + b = " + (a + b)); // 13
System.out.println("a - b = " + (a - b)); // 7
System.out.println("a * b = " + (a * b)); // 30
System.out.println("a / b = " + (a / b)); // 3 (integer division)
System.out.println("a % b = " + (a % b)); // 1 (remainder)
// Division with doubles
double x = 10.0, y = 3.0;
System.out.println("x / y = " + (x / y)); // 3.3333...
// Increment and decrement
int count = 5;
System.out.println("count++ = " + count++); // 5 (post-increment: use then increment)
System.out.println("count = " + count); // 6
System.out.println("++count = " + ++count); // 7 (pre-increment: increment then use)
System.out.println("count-- = " + count--); // 7 (post-decrement)
System.out.println("count = " + count); // 6
}
}
Relational operators compare two values and return a boolean result (true or false). They are commonly used in conditional statements and loops.
| Operator | Name | Example | Result | Description |
|---|---|---|---|---|
| == | Equal to | 5 == 3 | false | Checks if two values are equal |
| != | Not equal to | 5 != 3 | true | Checks if two values are not equal |
| > | Greater than | 5 > 3 | true | Checks if left is greater than right |
| < | Less than | 5 < 3 | false | Checks if left is less than right |
| >= | Greater than or equal | 5 >= 5 | true | Checks if left is greater than or equal to right |
| <= | Less than or equal | 5 <= 3 | false | Checks if left is less than or equal to right |
Logical operators are used to combine multiple boolean expressions or invert boolean values. They are essential for complex conditional logic.
| Operator | Name | Example | Result | Description |
|---|---|---|---|---|
| && | Logical AND | true && false | false | Returns true if both operands are true |
| || | Logical OR | true || false | true | Returns true if at least one operand is true |
| ! | Logical NOT | !true | false | Inverts the boolean value |
public class RelationalLogical {
public static void main(String[] args) {
int a = 10, b = 3;
// Relational operators
System.out.println("a > b: " + (a > b)); // true
System.out.println("a < b: " + (a < b)); // false
System.out.println("a == b: " + (a == b)); // false
System.out.println("a != b: " + (a != b)); // true
System.out.println("a >= 10: " + (a >= 10)); // true
System.out.println("b <= 5: " + (b <= 5)); // true
// Logical operators
boolean x = true, y = false;
System.out.println("x && y: " + (x && y)); // false (AND)
System.out.println("x || y: " + (x || y)); // true (OR)
System.out.println("!x: " + (!x)); // false (NOT)
// Complex conditions
int age = 25;
boolean hasLicense = true;
boolean canDrive = (age >= 18) && hasLicense;
System.out.println("Can drive: " + canDrive); // true
// Short-circuit evaluation
int num = 0;
if (num != 0 && (10 / num) > 1) { // Second part not evaluated
System.out.println("This won't execute");
}
System.out.println("No division by zero error!");
}
}
Assignment operators are used to assign values to variables. Java provides compound assignment operators that combine arithmetic operations with assignment.
| Operator | Example | Equivalent To | Description |
|---|---|---|---|
| = | a = 5 | - | Simple assignment |
| += | a += 5 | a = a + 5 | Add and assign |
| -= | a -= 5 | a = a - 5 | Subtract and assign |
| *= | a *= 5 | a = a * 5 | Multiply and assign |
| /= | a /= 5 | a = a / 5 | Divide and assign |
| %= | a %= 5 | a = a % 5 | Modulus and assign |
Bitwise operators perform operations on individual bits of integer types. They are used in low-level programming, optimization, and working with flags.
| Operator | Name | Example (a=5, b=3) | Binary | Result |
|---|---|---|---|---|
| & | Bitwise AND | 5 & 3 | 0101 & 0011 | 1 (0001) |
| | | Bitwise OR | 5 | 3 | 0101 | 0011 | 7 (0111) |
| ^ | Bitwise XOR | 5 ^ 3 | 0101 ^ 0011 | 6 (0110) |
| ~ | Bitwise NOT | ~5 | ~0101 | -6 |
| << | Left shift | 5 << 1 | 0101 << 1 | 10 (1010) |
| >> | Right shift | 5 >> 1 | 0101 >> 1 | 2 (0010) |
public class AssignmentBitwise {
public static void main(String[] args) {
// Assignment operators
int n = 10;
n += 5; // n = n + 5 → 15
n -= 3; // n = n - 3 → 12
n *= 2; // n = n * 2 → 24
n /= 4; // n = n / 4 → 6
n %= 4; // n = n % 4 → 2
System.out.println("n = " + n); // 2
// Bitwise operators
int a = 5; // 0101 in binary
int b = 3; // 0011 in binary
System.out.println("a & b = " + (a & b)); // 1 (0001)
System.out.println("a | b = " + (a | b)); // 7 (0111)
System.out.println("a ^ b = " + (a ^ b)); // 6 (0110)
System.out.println("~a = " + (~a)); // -6
System.out.println("a << 1 = " + (a << 1)); // 10 (multiply by 2)
System.out.println("a >> 1 = " + (a >> 1)); // 2 (divide by 2)
// Practical: check even/odd with bitwise AND
int num = 7;
if ((num & 1) == 0) {
System.out.println(num + " is even");
} else {
System.out.println(num + " is odd");
}
}
}
The ternary operator is a shorthand for if-else statements. Syntax: condition ? valueIfTrue : valueIfFalse
public class TernaryInstanceof {
public static void main(String[] args) {
// Ternary operator
int age = 20;
String status = (age >= 18) ? "Adult" : "Minor";
System.out.println(status); // Adult
// Find maximum
int a = 10, b = 20;
int max = (a > b) ? a : b;
System.out.println("Max: " + max); // 20
// instanceof operator
String str = "Hello";
System.out.println(str instanceof String); // true
System.out.println(str instanceof Object); // true
// Pattern matching (Java 16+)
Object obj = "World";
if (obj instanceof String s) {
System.out.println("Length: " + s.length());
System.out.println("Uppercase: " + s.toUpperCase());
}
}
}
Explore 500+ free tutorials across 20+ languages and frameworks.