Basics

Operators

Doing Math & Comparisons
💡 Operators are like the buttons on your calculator — +, -, *, / for maths, and ==, >, < for comparisons.

Arithmetic operators (+ - * / %) do maths on numbers. The % operator gives the "remainder" — like 10 % 3 = 1.

Relational operators (== != > < >= <=) compare two values and give true/false. Logical operators (&& || !) combine multiple conditions.

int a = 10, b = 3;
System.out.println(a % b);            // 1
System.out.println(a > b && b > 0);   // true
Operators are like the buttons on your calculator — +, -, *, / for maths, and ==, >, < for comparisons.
1 / 6
⚡ Quick Recap
  • + - * / % for maths
  • % gives the remainder
  • == > < compare, && || combine logic
On this page (5 subtopics)

+ - * / % handle basic maths. The behavior of division (/) depends on operand types — dividing two integers gives "integer division" (the decimal is cut off, so 7/2 = 3, not 3.5), but if even one operand is a double, the result is a decimal (7.0/2 = 3.5). % (modulo) gives the remainder, which can be confusing with negative numbers (-7 % 2 = -1 in Java, not 1).

Unary ++ and -- work in two ways — pre-increment (++x, increment first then use it) and post-increment (x++, use it first then increment). Using both in the same expression (like x = x++ + ++x) can produce confusing, undefined-ish behavior — never write code like that in practice.

int x = 5;
System.out.println(x++); // prints 5, फिर x = 6
System.out.println(++x); // x = 7 pehle, फिर prints 7
System.out.println(7 / 2);   // 3 (integer division)
System.out.println(7.0 / 2); // 3.5
⚠️Common Mistake: If you write int result = 5 / 2; expecting 2.5 but got 2, that's not a bug — Java is doing integer division. Fix: write (double) 5 / 2 (explicitly make one operand a double).

Relational operators (== != > < >= <=) compare two values and give a boolean result. Logical && and || "short-circuit" — meaning if the first operand alone decides the result, the second is never evaluated (essential for both performance and safety, like null-checks).

& and | also act like logical operators but do NOT short-circuit — both sides always get evaluated, needed or not. So if the second operand has a side-effect (like a method call) that should only run conditionally, always use && / ||, not & / |.

The ! (NOT) operator flips a boolean value — !true = false.

String s = null;
if (s != null && s.length() > 0) { // short-circuit: s.length() call hi nahi hoga agar s null hai
  System.out.println("Safe!");
}
💡Tip: Whenever you access a property/method along with a null-check, always use && to avoid a NullPointerException — order matters: null-check first, then access.

These operators work at the level of bits (0/1) — & (AND, 1 only if both are 1), | (OR, 1 if either is 1), ^ (XOR, 1 if exactly one is 1), ~ (NOT, flips every bit). << shifts left (each left shift acts like multiplying by 2), >> is a signed right shift (like dividing by 2, preserving the sign bit for negative numbers), and >>> is an unsigned right shift (fills even the sign bit of negative numbers with 0).

These operators are used in low-level programming, packing flags/permissions into a single int (like file read/write/execute permissions), or performance-critical bit-manipulation calculations. They're rare in everyday application code, but very common in interviews.

int a = 5;       // 0101
int b = 3;        // 0011
System.out.println(a & b);  // 1  (0001)
System.out.println(a | b);  // 7  (0111)
System.out.println(a ^ b);  // 6  (0110)
System.out.println(a << 1); // 10 (1010)
System.out.println(-8 >> 1);  // -4 (sign preserved)
System.out.println(-8 >>> 1); // bahut bada positive number

Compound assignment operators (+=, -=, *=, /=, %=) are shortcuts — x += 5 means x = x + 5. It's not just shorter to write — it also does an implicit cast that can be useful: byte b = 10; b += 5; works, but b = b + 5; gives a compile error (because b+5 becomes an int expression, which needs to be manually cast back to byte).

The ternary operator (condition ? valueIfTrue : valueIfFalse) is a compact if-else that fits into an expression — for when you just need to assign/return one value or another, based on a condition.

int marks = 75;
String result = (marks >= 40) ? "Pass" : "Fail";

byte b = 10;
b += 5;  // works — implicit narrowing cast hoti hai
// b = b + 5; // ye error dega bina explicit cast ke

When an expression has multiple operators, Java follows a fixed order to decide which runs first — this is called "precedence". Unary operators (++, --, !) have the highest precedence, then arithmetic (* / % first, then + -), then relational (< > ==), then logical (&& then ||), and assignment (=) last of all.

Whenever there's confusion, use parentheses () — they don't just make code more readable, they also explicitly override precedence and prevent bugs.

Precedence (High → Low)Operators
1. Unary++ -- ! ~
2. Multiplicative* / %
3. Additive+ -
4. Relational< > <= >=
5. Equality== !=
6. Logical AND&&
7. Logical OR||
8. Ternary? :
9. Assignment= += -= *= /=
⚠️Common Mistake: int result = 2 + 3 * 4; equals 14, not 20 — multiplication runs first. If you wanted 20, write: (2 + 3) * 4.