Core Java · 1. Java basics, lesson 3 of 9

Operators and expressions

Beginner3 min readall versions
Explain it forThe essentials plus production detail and pitfalls.

Java's operators, grouped:

  • Arithmetic: + - * / %. Integer division drops the fraction, so 7 / 2 is 3 and 7 % 2 (the remainder) is 1.
  • Increment: i++ uses the value and then adds one; ++i adds first.
  • Compound assignment: +=, -=, *=. They also cast back to the variable's type.
  • Comparison: == != < > <= >= produce a boolean.
  • Logical: && and || short-circuit, so the right side runs only when needed; ! negates.
  • Ternary: condition ? a : b picks one of two values.

Precedence: * / % before + -, comparisons before &&, and && before ||. When in doubt, add parentheses.

Mixing types promotes to the wider type, so int + double gives a double. Going narrower needs a cast: (int) 3.9 is 3.

Bitwise and shift operators

They work on the individual bits of integers:

  • & AND, | OR, ^ XOR (exclusive or), ~ NOT (flips every bit).
  • << shifts left (multiplies by 2 per step), >> shifts right keeping the sign, >>> shifts right filling with zeros.

You'll meet them in flags and permissions, hashing (HashMap computes the bucket with hash & (n - 1)) and fast checks such as "is this number odd?".

Java
int read = 1, write = 2, exec = 4;          // one bit per permission
int perms = read | write;                    // 3 (binary 011)
boolean canWrite = (perms & write) != 0;     // true
perms = perms & ~write;                      // remove write: 1

System.out.println(7 & 1);       // 1: odd
System.out.println(5 << 1);      // 10: times 2
System.out.println(-16 >> 2);    // -4: sign kept
System.out.println(-16 >>> 28);  // 15: zeros shifted in

Compound assignment hides a cast

x += y isn't exactly x = x + y: compound assignment casts the result back to the type of x. That makes some lines compile that look like they shouldn't, and can silently overflow.

Java
byte b = 10;
b += 5;            // compiles: means b = (byte) (b + 5)
// b = b + 5;      // compile error: b + 5 is an int

byte big = 120;
big += 10;         // silently wraps to -126

char c = 'A';
c += 1;            // 'B'

Example

Java
int a = 7, b = 2;
System.out.println(a / b);        // 3   (integer division)
System.out.println(a % b);        // 1   (remainder)
System.out.println(a / 2.0);      // 3.5 (double division)

int i = 5;
int x = i++;                      // x = 5, then i becomes 6
int y = ++i;                      // i becomes 7, then y = 7

String user = null;
if (user != null && user.length() > 3) {   // && stops before calling length()
    System.out.println("valid");
}

int marks = 72;
String result = marks >= 40 ? "Pass" : "Fail";
int rounded = (int) 3.99;         // 3: casting truncates
long big = 1_000_000L * 3_000;    // long avoids int overflow

Common mistake

Writing if (done = true) instead of if (done == true). With booleans it compiles, assigns true, and the branch always runs. Just write if (done).

Under the hood

& and | on booleans don't short-circuit, so both sides always run. Bitwise operators (& | ^ ~ << >> >>>) work on the bits of integers and appear in flags, hashing and performance code; >>> shifts in zeros while >> keeps the sign. Floating-point maths is approximate: 0.1 + 0.2 is 0.30000000000000004, which is why money uses BigDecimal.

Check yourself

What does 9 / 2 * 2 evaluate to in Java?

How this connects

Part of Java from zero.

Was this lesson helpful?

Finished reading? Mark it complete to track your progress.