Java Operators: Complete Guide with Examples
Java operators are special symbols that perform operations on variables and values. They are the building blocks of every Java expression — from simple math to complex logical decisions.
In this guide, you will learn all 8 types of operators in Java — Unary, Arithmetic, Relational, Bitwise, Logical, Ternary, Assignment, and Instanceof — with runnable code examples and their expected output.
What are Java Operators?
Java operators are used to perform mathematical or logical operations on operands. An operand is any value or variable that an operator acts on.
There are 8 types of operators in Java:
- Unary Operators
- Arithmetic Operators
- Relational Operators
- Bitwise Operators
- Logical Operators
- Ternary Operators
- Assignment Operators
- Instanceof Operator
1. Unary Operators
Unary operators operate on a single operand. They are commonly used to increment or decrement a value by 1.
There are two forms:
- Postfix —
i++,i-- - Prefix —
++i,--i
Postfix: The current value is used in the expression first, then incremented or decremented by 1.
Prefix: The value is incremented or decremented first, then used in the expression.
Example:
public class Test {
public static void main(String[] args) {
int i = 10;
// postfix increment
i++;
System.out.println(i); // 11
// prefix increment
++i;
System.out.println(i); // 12
// postfix decrement
i--;
System.out.println(i); // 11
// prefix decrement
--i;
System.out.println(i); // 10
}
}
2. Arithmetic Operators
Arithmetic operators perform mathematical operations on numeric values.
The arithmetic operators are:
+— addition-— subtraction*— multiplication/— division%— modulus (remainder)
Example:
public class Test {
public static void main(String[] args) {
int i = 10;
int j = 5;
System.out.println(i + j); // 15
System.out.println(i - j); // 5
System.out.println(i * j); // 50
System.out.println(i / j); // 2
System.out.println(i % j); // 0
}
}
3. Relational Operators
Relational operators compare two values and return true or
false based on the condition.
The relational operators are:
<— less than>— greater than<=— less than or equal to>=— greater than or equal to==— equal to!=— not equal to
Example:
public class Test {
public static void main(String[] args) {
int i = 10;
int j = 5;
System.out.println(i < j); // false
System.out.println(i > j); // true
System.out.println(i <= j); // false
System.out.println(i >= j); // true
System.out.println(i == j); // false
System.out.println(i != j); // true
}
}
4. Bitwise Operators
Bitwise operators work on individual bits of integer values. They can also be used with boolean operands.
The bitwise operators are:
&— Bitwise AND|— Bitwise OR^— Bitwise XOR~— Bitwise NOT (complement)
Bitwise AND (&): true only if both bits are 1.
Bitwise OR (|): false only if both bits are 0.
Bitwise XOR (^): true if the bits are different.
Bitwise NOT (~): flips every bit.
Example:
public class Test {
public static void main(String[] args) {
int i = 10; // binary: 1010
int j = 5; // binary: 0101
// Bitwise AND
// 1010
// 0101
// 0000 = 0
System.out.println(i & j); // 0
// Bitwise OR
// 1010
// 0101
// 1111 = 15
System.out.println(i | j); // 15
// Bitwise XOR
// 1010
// 0101
// 1111 = 15
System.out.println(i ^ j); // 15
// Bitwise NOT (complement)
System.out.println(~i); // -11
System.out.println(~j); // -6
}
}
5. Logical Operators
Logical operators determine the logical relationship between boolean expressions. Unlike bitwise operators, they work only on boolean values.
The logical operators are:
&&— Logical AND (short-circuits if first is false)||— Logical OR (short-circuits if first is true)!— Logical NOT
Example:
public class Test {
public static void main(String[] args) {
int i = 10;
int j = 5;
System.out.println(i > j && i < j); // false
System.out.println(i > j || i > j); // true
System.out.println(!(i > j || i > j)); // false
}
}
6. Ternary Operator
The ternary operator is a shorthand version of the if-else statement. It is
the only operator in Java that takes three operands.
Syntax:
(condition) ? valueIfTrue : valueIfFalse;
Example:
public class Test {
public static void main(String[] args) {
String msg = (10 > 5) ? "right" : "wrong";
System.out.println(msg); // right
int result1 = (10 > 5) ? 10 : 5;
System.out.println(result1); // 10
float result2 = (10 > 5) ? 10.0f : 5.0f;
System.out.println(result2); // 10.0
boolean result3 = (10 > 5) ? true : false;
System.out.println(result3); // true
// Nested ternary
int result4 = (10 > 5) ? (10 != 0) ? 10 : 0 : 5;
System.out.println(result4); // 10
}
}
7. Assignment Operators
Assignment operators assign values to variables.
The assignment operators are:
=— assign+=— add and assign-=— subtract and assign*=— multiply and assign/=— divide and assign%=— modulus and assign
Example:
public class Test {
public static void main(String[] args) {
int i = 10;
int j = 5;
int k = i + j;
System.out.println(k); // 15
i += j; // i = i + j
System.out.println(i); // 15
i -= j; // i = i - j
System.out.println(i); // 10
i *= j; // i = i * j
System.out.println(i); // 50
i /= j; // i = i / j
System.out.println(i); // 10
i %= j; // i = i % j
System.out.println(i); // 0
}
}
8. Instanceof Operator
The instanceof operator checks whether an object is an instance of a
specific class or implements a specific interface.
It returns true or false.
Example:
class Animal {
}
class Dog extends Animal {
}
class Lion extends Animal {
}
public class Test {
public static void main(String[] args) {
Animal animal = new Dog();
System.out.println(animal instanceof Dog); // true
animal = new Lion();
System.out.println(animal instanceof Lion); // true
}
}
Use case: instanceof is commonly used before type casting
to avoid ClassCastException at runtime.
Frequently Asked Questions
What are Java operators?
Java operators are special symbols that perform operations on operands (variables or values). They are used for mathematical calculations, comparisons, logical decisions, bit manipulation, and assignments.
What is the difference between prefix and postfix operators in Java?
In postfix (i++), the current value is used first and then incremented. In
prefix (++i), the value is incremented first and then used. This matters in
expressions where the result is assigned or passed to another operation.
What is the difference between bitwise and logical operators in Java?
Bitwise operators (&, |, ^,
~) work on individual bits and evaluate both operands. Logical operators
(&&, ||, !) work on boolean values and
short-circuit — the second operand is not evaluated if the result is already determined.
What is the ternary operator in Java?
The ternary operator (condition ? valueIfTrue : valueIfFalse) is a shorthand
for if-else. It returns one of two values based on a boolean condition and
is the only operator in Java that takes three operands.
What does the instanceof operator do in Java?
The instanceof operator checks whether an object is an instance of a
specific class or implements a specific interface. It returns true or
false and helps avoid ClassCastException when performing type
casting.
Conclusion
Java operators are fundamental to writing any program. You have now learned all 8 types — Unary, Arithmetic, Relational, Bitwise, Logical, Ternary, Assignment, and Instanceof — with practical examples and their output.
The next step is to practice combining these operators in real programs. Pay special
attention to prefix vs postfix, and to the difference between bitwise (&)
and logical (&&) — these are the most common sources of confusion
for beginners.
Related topics: Java Data Types, Java Type Casting, Java Control Flow Statements, Java Conditional Operators, Java Operator Precedence, Java Variables
No comments:
Post a Comment