Operators
Operators
A school program needs reliable data and clear rules. Operators combine or compare values. A marks report can add subject scores and compare the total with a threshold; a comparison produces a boolean value.
Understand
Operators combine or compare values.
Apply
Trace the Java example, predict its output and explain the result before running it.
Avoid mistakes
Use equals to compare String contents. The == operator compares object references rather than generally comparing their text.
Operators in Java
Operators in Java are special symbols used to perform actions on variables and values. They help you do things like math calculations, compare numbers, assign values, and make decisions.
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.
Important boundaries and mistakes
Use equals to compare String contents. The == operator compares object references rather than generally comparing their text.
Try the school example
Before running this Java program, write down each line you expect to see. Identify the value, branch, object or exception responsible for each line. Then explain whether the following assumption is valid: Use equals to compare String contents.
Java 17 · Main.java · complete practice program
public class Main {
public static void main(String[] args) {
int total = 35 + 40;
System.out.println(total >= 70);
}
}
Now change one thing: Change the two marks to 30 and 35. Predict the new behavior before checking the worked variation.
Worked answer and reasoning
Java 17 · Main.java · source repeated for this answer
public class Main {
public static void main(String[] args) {
int total = 35 + 40;
System.out.println(total >= 70);
}
}
Expected output
trueOperators combine or compare values. A marks report can add subject scores and compare the total with a threshold; a comparison produces a boolean value.
Use equals to compare String contents. The == operator compares object references rather than generally comparing their text.
Run with javac Main.java, then java Main. The declaration of Main names the class; main starts the example, and println writes a complete output line.
Worked variation: Change the two marks to 30 and 35. Their total is 65, so total >= 70 is false.
Check your understanding
What to remember
Operators combine or compare values. A marks report can add subject scores and compare the total with a threshold; a comparison produces a boolean value. Use equals to compare String contents. The == operator compares object references rather than generally comparing their text.
Related explanations: Java Variables, Java If Statement - Syntax and Examples, Java Class - Blueprint of Objects, Exceptions in Java Introduction.
Supporting documentation: Official Java learning guide and Java 17 language specification.
Reaching the recap and completing the quizzes are separate milestones.