Curriculum / Java / Lesson 04

Operators in Java

Java's operators closely mirror C and C++: arithmetic (+ - * / %), relational (== != > <), and logical (&& || !) — but comparisons always produce a real boolean, never an int..

4 of 11
beginner 📖 5 min read 🎯 1 question

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  !a

Example

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.

Quick Quiz

1. What type does a comparison like (a > b) always evaluate to in Java?

Back to curriculum