Arithmetic
Java
int a = 10, b = 3;
System.out.println(a + b); // 13
System.out.println(a - b); // 7
System.out.println(a * b); // 30
System.out.println(a / b); // 3 ← integer division
System.out.println(a % b); // 1 ← remainderIncrement / decrement
Java
int i = 5;
System.out.println(i++); // prints 5, then i becomes 6
System.out.println(++i); // i becomes 7, then prints 7Assignment
Java
int x = 10;
x += 5; // 15
x -= 3; // 12
x *= 2; // 24
x /= 4; // 6
x %= 4; // 2Comparison
== != > < >= <= — all return boolean.
The == trap with objects
For primitives, == compares values. For objects, it compares
references — whether they are the same object in memory.
Java
String a = "hello";
String b = "hello";
System.out.println(a == b); // true — both point to the same pooled literal
String c = new String("hello");
System.out.println(a == c); // false! different objects
System.out.println(a.equals(c)); // true — compares contentsAlways use .equals() to compare Strings and other objects. This is
the single most common beginner bug in Java.
Logical
Java
boolean p = true, q = false;
System.out.println(p && q); // false — AND
System.out.println(p || q); // true — OR
System.out.println(!p); // false — NOT&& and || short-circuit: if the left side already decides the
answer, the right side is never evaluated. This makes null checks safe:
Java
if (user != null && user.isActive()) { } // safeTernary
Java
String status = (age >= 18) ? "adult" : "minor";