Skip to content
  • Operators & Precedence · Java

Operators & Precedence

LearnViaNotes @learnvianotes· 4 min read

Java's operators are C-like, with a few Java-specific twists: >>>, string +, instanceof, and strict boolean conditions.

KindOperatorsNotes
Arithmetic+ - * / %Integer / truncates toward zero; % takes the sign of the dividend
Unary+ - ++ -- !, ~Prefix ++i increments then yields; postfix i++ yields then increments
Relational== != < <= > >=== on objects compares references
Logical&& || !Short-circuit; operands must be boolean
Bitwise& | ^ ~On boolean operands &/| evaluate both sides
Shift<< >> >>>>> keeps the sign, >>> fills with zeros
Assignment= += -= *= /= %= &= |= ^= <<= >>= >>>=Compound forms cast implicitly
Ternarycond ? a : bAn expression — yields a value
Typeinstanceofobj instanceof String s binds a variable (Java 16+)

Arithmetic details

Java
public class Main {
    public static void main(String[] args) {
        System.out.println(7 / 2 + " " + -7 / 2 + " " + 7 % 3 + " " + -7 % 3);
        System.out.println(Math.floorDiv(-7, 2) + " " + Math.floorMod(-7, 3));
        System.out.println(5.5 % 2);
        int i = 5;
        int a = i++;          // a = 5, then i = 6
        int b = ++i;          // i = 7, then b = 7
        System.out.println(a + " " + b + " " + i);
        String s = "Sum: " + 1 + 2;        // left to right: string concat
        String t = 1 + 2 + " is the sum";  // 3 first, then concat
        System.out.println(s + " | " + t);
    }
}
Output
3 -3 1 -1
-4 2
1.5
5 7 7
Sum: 12 | 3 is the sum

Short-circuit evaluation

Java
public class Main {
    static boolean check(String label, boolean value) {
        System.out.println("evaluating " + label);
        return value;
    }

    public static void main(String[] args) {
        String s = null;
        if (s != null && s.length() > 3) System.out.println("long");   // safe: right side skipped
        System.out.println(check("A", false) && check("B", true));
        System.out.println(check("C", false) & check("D", true));       // non-short-circuit
    }
}
Output
evaluating A
false
evaluating C
evaluating D
false

Bitwise & shift operators

Java
public class Main {
    public static void main(String[] args) {
        int a = 0b1100, b = 0b1010;
        System.out.println((a & b) + " " + (a | b) + " " + (a ^ b) + " " + ~a);
        System.out.println((1 << 10) + " " + (-16 >> 2) + " " + (-16 >>> 28));
        int n = 12;
        System.out.println("even? " + ((n & 1) == 0));
        System.out.println("power of two? " + (n > 0 && (n & (n - 1)) == 0));
        System.out.println("set bits: " + Integer.bitCount(n));
        int x = 3, y = 9;
        x ^= y; y ^= x; x ^= y;                  // XOR swap
        System.out.println(x + " " + y);
    }
}
Output
8 14 6 -13
1024 -4 15
even? true
power of two? false
set bits: 2
9 3

Precedence (high → low)

LevelOperators
Postfixexpr++ expr--
Unary++expr --expr + - ~ ! and casts
Multiplicative* / %
Additive+ -
Shift<< >> >>>
Relational< > <= >= instanceof
Equality== !=
Bitwise& then ^ then |
Logical&& then ||
Ternary? :
Assignment= += -= … (right-associative)
Java
public class Main {
    public static void main(String[] args) {
        System.out.println(2 + 3 * 4);
        System.out.println((2 + 3) * 4);
        System.out.println(1 + 2 << 1);           // + binds tighter than <<
        System.out.println(true || false && false);   // && before ||
        int score = 72;
        String grade = score >= 90 ? "A" : score >= 70 ? "B" : "C";
        System.out.println(grade);
    }
}
Output
14
20
6
true
B

What's the difference between & and &&?

What is >>>?

What does System.out.println(1 + 2 + "3" + 4 + 5) print?

Free for students and teachers

Write notes like this one

Cards for every idea, quizzes and flashcards, read aloud — on the web and your phone.

Something wrong with this note? Open it to report