Operators
Java's operators closely mirror C and C++: arithmetic (+ - * / %), relational (== != > <), and logical (&& || !) — but comparisons always produce a real boolean, never an int.
💡 Key insight
In Java, if (5) is a compile error — conditions must be real booleans, unlike C's "any non-zero is true" rule.
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
a > b evaluates to a real boolean value, printed here as true.
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));
}
}outputSum: 13
Remainder: 1
Greater: true
Try it yourself
Copy this example into the Java compiler and run it — try changing a value and see what happens.
