Definition
📌 Operators
Java's operators closely mirror C and C++: arithmetic (+ - * / %), relational (== != > <), and logical (&& || !) — but comparisons always produce a real boolean, never an int..
Syntax
syntax
a + b a - b a * b a / b a % b
a == b a != b a > b a < b
a && b a || b !aExample
java
public class Main {
public static void main(String[] args) {
int a = 10, b = 3;
System.out.println("Sum: " + (a + b));
System.out.println("Remainder: " + (a % b));
System.out.println("Greater: " + (a > b));
}
}Output
outputSum: 13
Remainder: 1
Greater: true
Important Points
- Comparison operators always return a
boolean(true/false), never 0 or 1. - Integer division truncates, just like in C:
7 / 2is3. &&and||short-circuit, same as C/C++.- The ternary operator
condition ? a : bworks the same way as in C/C++.
Common Mistakes
- Trying to use an
intdirectly as a condition — Java requires a genuineboolean. - Expecting
7 / 2to give a decimal when both operands areint. - Confusing
=and==in a condition — Java at least won't compile most of these mistakes as a safety net.
Interview Tip
🎯 Before your interview
Know that Java's strict boolean typing prevents a whole class of C-style bugs where an accidental assignment (
=) inside an if silently "works".
