Java Basics
Operators
Complete lesson for Operators using LEARNCODEGURU’s 6-level learning model — from basics to advanced practice, with Java OOP and Collections where relevant.
Basics — Operators
Section: Java Basics. Arithmetic, relational, logical, bitwise, assignment, ternary, and instanceof — with precedence and short-circuit rules.
What are operators?
- Operators compute a value from one or more operands. Java has no pointer-dereference or scope-resolution operator (
*/::as in C++). - Integer
/and%truncate toward zero.%works for integers; for floating point it is allowed but rarely used in interviews.
Classification (overview)
| Category | Operators | Purpose |
|---|---|---|
| Arithmetic | + - * / % ++ -- | Math and increment |
| Relational | == != > < >= <= | Compare values |
| Logical | && || ! | Boolean combine (short-circuit) |
| Bitwise / shift | & | ^ ~ << >> >>> | Bit operations on integers |
| Assignment | = += -= *= /= %= &= |= ^= <<= >>= >>>= | Store or update |
| Other | ?:, instanceof, (), new | Ternary, type test, cast, allocate |
Arithmetic, relational, logical
&&and||short-circuit: the right operand is skipped if the result is already known.&and|on booleans always evaluate both sides.++/--as prefix return the updated value; as postfix they return the old value.
public class OperatorBasics {
public static void main(String[] args) {
int a = 10, b = 3;
System.out.println(a / b); // 3
System.out.println(a % b); // 1
System.out.println(a > b && b != 0);
int max = (a > b) ? a : b;
System.out.println(max);
}
} Bitwise operators
- Work on two’s-complement integers.
>>>is unsigned right shift (fills with zeros) — Java has this; C++ does not spell it the same way. n & 1tests odd;n << 1multiplies by 2 for non-overflowing values;n ^ nis 0.
| Op | Name | Example idea |
|---|---|---|
& | AND | Clear / test bits |
| | OR | Set bits |
^ | XOR | Toggle bits; find unique number |
~ | NOT | Flip all bits |
<< | Left shift | Shift toward high bits |
>> | Signed right shift | Keeps sign bit |
>>> | Unsigned right shift | Fills with 0 |
Advanced — Operators
Precedence, associativity, and instanceof
- Unary > arithmetic > shift > relational > equality > bitwise AND/XOR/OR > logical &&/|| > ternary > assignment.
- When unsure, use parentheses. Interviewers love
i = i++style traps — the value ofi++is the oldi, then assignment overwrites. obj instanceof Typeisfalsefornull. Pattern matching (if (obj instanceof String s)) is standard in Java 16+.- String
+concatenates; if either operand isString, the other is converted viaString.valueOf.
Tricky Interview Points
Q1) What is the difference between && and & for booleans?
Answer && short-circuits; & always evaluates both operands. Use && in conditions to avoid NPEs and extra work.
Q2) What does >>> do?
Answer Unsigned right shift: bits move right and vacated high bits become 0, even for negative ints.
Q3) What is 7 / 2 and 7 % 2 in Java?
Answer 3 and 1. Integer division truncates toward zero.
Q4) Does instanceof work on null?
Answer null instanceof AnyType is false and does not throw.
Q5) Why is i = i++ a trap?
Answer The postfix expression yields the old value, then i is incremented, then the assignment writes the old value back.
Q6) Can you overload operators in Java?
Answer No, except that + is overloaded by the language for String concatenation. You cannot define your own operator overloads.
Simple Programs
Bitwise demo
public class BitwiseDemo {
public static void main(String[] args) {
int a = 12; // 1100
int b = 10; // 1010
System.out.println(a & b); // 8
System.out.println(a | b); // 14
System.out.println(a ^ b); // 6
System.out.println(a << 1); // 24
int n = 7;
System.out.println((n & 1) == 1 ? "odd" : "even");
}
} Problem Solving Tricks
- Use
&&/||inifconditions; reserve&/|for bit masks (or rare non-short-circuit needs). - Parenthesize mixed shift and arithmetic expressions.
n & (n - 1)clears the lowest set bit — a common interview trick.- Do not use
+for heavy string building in a loop; useStringBuilder.
Practice Programs List
- Print AND, OR, XOR, left shift, and both right shifts of two integers.
- Write a program that finds the maximum of three ints using only the ternary operator.
- Demonstrate short-circuit: skip a method call on the right of
&&when the left is false. - Check whether an int is a power of two using
n > 0 && (n & (n - 1)) == 0. - Predict
int i = 5; i = i++;then run it.