Skip to main content

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.

L1

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)

CategoryOperatorsPurpose
Arithmetic+ - * / % ++ --Math and increment
Relational== != > < >= <=Compare values
Logical&& || !Boolean combine (short-circuit)
Bitwise / shift& | ^ ~ << >> >>>Bit operations on integers
Assignment= += -= *= /= %= &= |= ^= <<= >>= >>>=Store or update
Other?:, instanceof, (), newTernary, 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 & 1 tests odd; n << 1 multiplies by 2 for non-overflowing values; n ^ n is 0.
OpNameExample idea
&ANDClear / test bits
|ORSet bits
^XORToggle bits; find unique number
~NOTFlip all bits
<<Left shiftShift toward high bits
>>Signed right shiftKeeps sign bit
>>>Unsigned right shiftFills with 0
L2

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 of i++ is the old i, then assignment overwrites.
  • obj instanceof Type is false for null. Pattern matching (if (obj instanceof String s)) is standard in Java 16+.
  • String + concatenates; if either operand is String, the other is converted via String.valueOf.
L3

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.

L4

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");
    }
}
L5

Problem Solving Tricks

  • Use && / || in if conditions; 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; use StringBuilder.
L6

Practice Programs List

  1. Print AND, OR, XOR, left shift, and both right shifts of two integers.
  2. Write a program that finds the maximum of three ints using only the ternary operator.
  3. Demonstrate short-circuit: skip a method call on the right of && when the left is false.
  4. Check whether an int is a power of two using n > 0 && (n & (n - 1)) == 0.
  5. Predict int i = 5; i = i++; then run it.