← Java tutorial

Java

Operators

Arithmetic (+ - * /), comparison (== != > <), and logical (&& || !) operators work much like other C-family languages.

Example: a boolean expression

public class Main {
    public static void main(String[] args) {
        int score = 85;
        boolean passed = score >= 50 && score <= 100;
        System.out.println("Passed: " + passed);
    }
}
Passed: true

One Java-specific trap worth knowing early: == compares object references for non-primitive types like String, not their actual text content — two separately-created strings with identical text can compare as not equal with ==. Strings are almost always compared with .equals() instead: name.equals("Ada"), not name == "Ada".

Example
public class Main {
    public static void main(String[] args) {
        int score = 85;
        boolean passed = score >= 50 && score <= 100;
        System.out.println("Passed: " + passed);
    }
}
Output
Passed: true