Java Operators
Arithmetic, Logical, Bitwise & More
Operators in Java
Operators in Java act like bridges between variables and values. They perform computations, comparisons, assignments, and logical decisions. Understanding these operators is fundamental before you dive into conditions, loops, and more complex logic in Java.
In this guide, we'll walk through all the primary types of Java operators, explaining each with easy-to-understand code examples and detailed output explanations.
Types of Java Operators
- Arithmetic Operators
- Relational (Comparison) Operators
- Logical Operators
- Assignment Operators
- Unary Operators
- Bitwise Operators
- Ternary Operator
Arithmetic Operators in Java
Arithmetic operators are used to perform basic mathematical operations.
Operator | Symbol | Description |
---|---|---|
Addition | + | Adds two values |
Subtraction | - | Subtracts right from left |
Multiplication | * | Multiplies two values |
Division | / | Divides left by right |
Modulus | % | Returns remainder |
public class ArithmeticExample {
public static void main(String[] args) {
int a = 10, b = 3;
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));
System.out.println("a % b = " + (a % b));
}
}
a + b = 13
a - b = 7
a * b = 30
a / b = 3
a % b = 1
Assignment Operators in Java
These operators assign values to variables.
Operator | Symbol | Description |
---|---|---|
Assign | = | Assigns right side to left variable |
Add and Assign | += | Performs addition then assigns |
Subtract and Assign | -= | Performs subtraction then assigns |
Multiply and Assign | *= | Performs multiplication then assigns |
Divide and Assign | /= | Performs division then assigns |
Modulus and Assign | %= | Performs modulus then assigns |
public class AssignmentExample {
public static void main(String[] args) {
int a = 5;
a += 3;
a *= 2;
System.out.println("Final value of a: " + a);
}
}
Final value of a: 16
Relational Operators in Java
Relational operators compare two values and return a boolean result.
Operator | Symbol | Description |
---|---|---|
Equal To | == | Checks if two values are equal |
Not Equal To | != | Checks if two values are not equal |
Greater Than | > | Checks if left is greater |
Less Than | < | Checks if left is smaller |
Greater Than or Equal | >= | Checks if left is greater or equal |
Less Than or Equal | <= | Checks if left is smaller or equal |
public class RelationalExample {
public static void main(String[] args) {
int x = 10, y = 20;
System.out.println("x == y: " + (x == y));
System.out.println("x != y: " + (x != y));
System.out.println("x < y: " + (x < y));
System.out.println("x > y: " + (x > y));
}
}
x == y: false
x != y: true
x < y: true
x > y: false
Logical Operators in Java
Logical operators help in combining multiple boolean expressions.
Operator | Symbol | Description |
---|---|---|
Logical AND | && | True if both operands are true |
Logical OR | || | True if any operand is true |
Logical NOT | ! | Inverts the boolean value |
public class LogicalExample {
public static void main(String[] args) {
int a = 5, b = 10;
System.out.println((a > 0) && (b > 0));
System.out.println((a > 0) || (b < 0));
System.out.println(!(a == b));
}
}
true
true
true
Unary Operators in Java
Unary operators operate on a single operand.
Operator | Symbol | Description |
---|---|---|
Unary Plus | + | Indicates positive value |
Unary Minus | - | Negates the value |
Increment | ++ | Increases value by 1 |
Decrement | -- | Decreases value by 1 |
Logical NOT | ! | Inverts a boolean |
public class UnaryExample {
public static void main(String[] args) {
int a = 5;
System.out.println("a = " + a);
System.out.println("++a = " + (++a));
System.out.println("a-- = " + (a--));
System.out.println("Final a = " + a);
}
}
a = 5
++a = 6
a-- = 6
Final a = 5
Bitwise Operators in Java
Bitwise operators work on bits and perform bit-by-bit operations.
Operator | Symbol | Description |
---|---|---|
AND | & | Bitwise AND |
OR | | | Bitwise OR |
XOR | ^ | Bitwise exclusive OR |
Complement | ~ | Bitwise NOT |
Left Shift | << | Shift bits left |
Right Shift | >> | Shift bits right |
public class BitwiseExample {
public static void main(String[] args) {
int a = 5, b = 3;
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: " + (~a));
System.out.println("a << 1: " + (a << 1));
System.out.println("a >> 1: " + (a >> 1));
}
}
a & b: 1
a | b: 7
a ^ b: 6
~a: -6
a << 1: 10
a >> 1: 2
Things to Remember
- Operator precedence matters – always use parentheses if unsure. We cover this in our very next topic.
- Logical operators short-circuit (&& and ||) – they may skip evaluating the second condition.
- Bitwise and logical operators are not the same (& vs &&).
- Unary post-increment and pre-increment behave differently.
QUIZ
Question 1:Which of the following outputs will result from the given code snippet?
int a = 10, b = 3;
System.out.println("a % b = " + (a % b));
int a = 10, b = 3;
System.out.println("a % b = " + (a % b));
Question 2:The statement a += 3
is equivalent to a = a + 3
in Java.
Question 3:Which of the following are valid relational operators in Java?
Question 4:What is the output of the following code?
int a = 5;
System.out.println(++a);
System.out.println(a--);
System.out.println(a);
int a = 5;
System.out.println(++a);
System.out.println(a--);
System.out.println(a);