Operators
Arithmetic / comparison / logic / bitwise / ternary / instanceof
Arithmetic Operators
+ - * / %. Integer / is integer division, remainder via %. If either operand is floating point, the result is floating point. Integer division by 0 throws ArithmeticException; floating-point division by 0 yields Infinity or NaN.
// Arithmetic.java
public class Arithmetic {
public static void main(String[] args) {
int a = 7, b = 3;
System.out.println(a + b); // 10
System.out.println(a - b); // 4
System.out.println(a * b); // 21
System.out.println(a / b); // 2 (integer division)
System.out.println(a % b); // 1
double x = 7.0;
System.out.println(x / b); // 2.3333...
System.out.println(x / 0); // Infinity
System.out.println(0.0 / 0); // NaN
}
}Increment / Decrement
i++ (postfix) uses the current value first, then adds 1; ++i (prefix) adds 1 first, then uses the new value. Prefer them as standalone statements rather than embedded in expressions.
// IncDec.java
public class IncDec {
public static void main(String[] args) {
int i = 5;
int a = i++; // a=5, i=6 (postfix)
int b = ++i; // i=7, b=7 (prefix)
System.out.println(i + " " + a + " " + b); // 7 5 7
}
}Comparison and Logic (Short-Circuit)
Comparison operators return boolean. Logical && and || short-circuit: when the left side already determines the result, the right side is skipped. This enables the safe "check for null before calling a method" pattern.
// Logic.java
public class Logic {
public static void main(String[] args) {
String s = null;
// short-circuit: left is null, right is not evaluated, avoiding a NullPointerException
if (s != null && s.length() > 0) {
System.out.println("non-empty");
} else {
System.out.println("empty or null");
}
int age = 25;
boolean adultOk = age >= 18 && age < 60;
System.out.println(adultOk);
}
}Bitwise Operations
& bitwise AND, | bitwise OR, ^ XOR, ~ NOT, << left shift, >> arithmetic right shift (sign-extended), >>> logical right shift (zero-filled).
// Bits.java
public class Bits {
public static void main(String[] args) {
int flags = 0b1010;
System.out.println(Integer.toBinaryString(flags & 0b1100)); // 1000
System.out.println(Integer.toBinaryString(flags | 0b0101)); // 1111
System.out.println(Integer.toBinaryString(flags ^ 0b1111)); // 101
System.out.println(Integer.toBinaryString(flags << 2)); // 101000
System.out.println(Integer.toBinaryString(-1 >>> 28)); // 1111 (zero-filled high bits)
}
}Ternary Operator ?:
// Ternary.java
public class Ternary {
public static void main(String[] args) {
int score = 75;
String grade = score >= 60 ? "pass" : "fail";
System.out.println(grade);
// nesting (poor readability; avoid more than two levels)
String level = score >= 90 ? "A" : score >= 60 ? "B" : "F";
System.out.println(level);
}
}instanceof and Pattern Variables (Java 16)
instanceof tests whether an object is of a given type. Java 16+ lets you bind it to a pattern variable at the same time, removing the later cast.
// InstanceOf.java
public class InstanceOf {
public static void describe(Object obj) {
// old style: test, then cast
// if (obj instanceof String) {
// String s = (String) obj;
// System.out.println("string length=" + s.length());
// }
// new style: Java 16+ pattern variable
if (obj instanceof String s) {
System.out.println("string length=" + s.length());
} else if (obj instanceof Integer n && n > 0) {
System.out.println("positive int=" + n);
} else {
System.out.println("other: " + obj);
}
}
public static void main(String[] args) {
describe("hello");
describe(42);
describe(3.14);
}
}Compound Assignment
+= -= *= /= %= &= |= ^= <<= >>= >>>=: a += b is equivalent to a = (type of a)(a + b) — note the built-in cast.
// Compound.java
public class Compound {
public static void main(String[] args) {
int x = 10;
x += 5; // 15
x *= 2; // 30
x >>= 1; // 15
System.out.println(x);
// note: byte b = 10; b = b + 1; fails (int can't implicitly convert to byte)
// but b += 1; works — compound assignment has a built-in cast
byte b = 10;
b += 1; // OK
System.out.println(b);
}
}Operator Precedence Cheat Sheet
High to low (left to right at the same level): unary (!, ~, ++, --, +/-) > multiply/divide/mod (* / %) > add/subtract (+ -) > shift (<< >> >>>) > comparison (< > <= >=, instanceof) > equality (== !=) > bitwise AND & > bitwise XOR ^ > bitwise OR | > logical AND && > logical OR || > ternary ?: > assignment = += -= …