Operators
Master Java operators — arithmetic, comparison, logical, bitwise, and the ternary operator — with the precedence rules that cause subtle bugs.
Last reviewed: January 2025
🔍 What Is It?
Arithmetic Operators
- →+, -, *, /, % (modulo)
- →Integer division truncates: 7/2 = 3
- →Modulo works on negatives: -7 % 2 = -1
Comparison Operators
- →==, !=, <, >, <=, >=
- →For objects, == compares references, not values
Logical Operators
- →&& (AND), || (OR), ! (NOT) — short-circuit evaluation
- →& (AND), | (OR) — non-short-circuit, always evaluates both sides
Bitwise Operators
- →& (AND), | (OR), ^ (XOR), ~ (NOT)
- →<< (left shift), >> (signed right shift), >>> (unsigned right shift)
Assignment Operators
- →=, +=, -=, *=, /=, %=, &=, |=, ^=
Other
- →instanceof — type check (Java 16+: pattern matching)
- →Ternary ? : — inline if-else
- →+ — string concatenation when one operand is String
🎯 Why Do We Need It?
Operators are the fundamental building blocks of any computation. Understanding short-circuit evaluation prevents NullPointerExceptions. Understanding integer division prevents calculation bugs. Understanding bitwise operators is essential for working with flags, permissions, and network protocols.
🏗️ What Happens in Real Projects?
Real enterprise usage:
- →Null-safe checks: if (user != null && user.isActive()) — short-circuit prevents NPE
- →Permission flags: if ((permissions & READ_FLAG) != 0) — bitwise AND for flag checking
- →Pagination: int offset = (page - 1) * pageSize
- →Modulo for cycling: int index = counter % array.length
- →Pattern matching instanceof (Java 16+): if (obj instanceof String s) { s.toUpperCase(); }
⚙️ How It Works
Operator Precedence (high to low, simplified)
- Postfix: expr++, expr--
- Unary: ++expr, --expr, +, -, !, ~
- Multiplicative: *, /, %
- Additive: +, -
- Shift: <<, >>, >>>
- Relational: <, >, <=, >=, instanceof
- Equality: ==, !=
- Bitwise AND: &
- Bitwise XOR: ^
- Bitwise OR: |
- Logical AND: &&
- Logical OR: ||
- Ternary: ? :
- Assignment: =, +=, etc.
Rule of thumb: when in doubt, use parentheses. Explicit is always better than relying on precedence.
💻 Example
Operators in Real Code — Practical operator usage patterns and common pitfalls.
🤔 Common Confusions
❓ What is the difference between & and && ?
&& is short-circuit AND — if the left side is false, the right side is NOT evaluated. & is bitwise AND (also works as logical AND but always evaluates both sides). Use && for boolean logic (it is safer and more efficient). Use & only when you explicitly need both sides evaluated or for bitwise operations on integers.
❓ Why does "hello" + 1 + 2 give "hello12" but 1 + 2 + "hello" give "3hello"?
The + operator evaluates left to right. "hello" + 1 = "hello1" (String concat), then "hello1" + 2 = "hello12". But 1 + 2 = 3 (int addition), then 3 + "hello" = "3hello" (String concat). Always use parentheses or StringBuilder when mixing types.
❓ What does the >>> operator do that >> does not?
>> is signed right shift — it preserves the sign bit (fills with 1 for negative numbers). >>> is unsigned right shift — it always fills with 0. For positive numbers they are identical. >>> is used in hash functions and bit manipulation algorithms.
🚨 Common Production Issues
Integer division returning 0 unexpectedly — int/int always truncates. A common bug: int pct = count / total * 100 always gives 0 if count < total. Fix: double pct = (double) count / total * 100.
NullPointerException from non-short-circuit evaluation — using & instead of && means the right side is evaluated even when the left is false, causing NPE on null object.
Overflow in arithmetic — int + int can overflow silently. For large calculations, use long or BigInteger.
✅ Best Practices
Always use && and || (short-circuit) for boolean logic, not & and |.
Use parentheses to make operator precedence explicit, not implicit.
Use Math.floorDiv() and Math.floorMod() for consistent behavior with negative numbers.
Prefer pattern matching instanceof (Java 16+) over cast-after-check.
Avoid nested ternary operators — use if/else for readability.
🏛️ Architect's Note
Bitwise operations are used in high-performance code: hash functions, cryptography, network protocol parsing, and permission systems. In business logic, prefer enums or EnumSet over int flags for type safety. EnumSet is internally implemented with bitwise operations but gives you type safety and readability.
💬 Frequently Asked Questions
🎯 Key Takeaways
🔗 Related Topics
🎯 Practice this topic in interviews
Core Java Interview Prep
Practice interview questions that connect directly to this technology track.
↗ Where this topic fits
Core Java Technology Guide
Return to the Core Java learning hub.
Backend Engineer Career Path
A structured journey through Core Java, Spring Boot, production backend engineering, and interview preparation.
Full Stack Java Developer Career Path
A guided route through frontend foundations, Java backend engineering, delivery workflows, and interview readiness.
Sources
Sources / References
Use these Core Java references to verify the concept in official docs and continue into the most relevant SplashRide pages.
Official documentation
Related SplashRide pages
SplashRide Core Java learning path
Browse the full Core Java topic collection and learning order on SplashRide.
Core Java Interview Prep
Practice Core Java interview questions and scenario-based answers.
Backend Engineer Career Path
A structured journey through Core Java, Spring Boot, production backend engineering, and interview preparation.
Full Stack Java Developer Career Path
A guided route through frontend foundations, Java backend engineering, delivery workflows, and interview readiness.
Data Types
Master Java data types — primitives vs objects, type casting, autoboxing, and the gotchas that cause bugs in production.
Classes & Objects
The foundation of Java OOP — how classes define blueprints and objects are instances, with constructors, fields, methods, and the this keyword.