JAVA • Beginner

Java Operators

Operators are special symbols that perform operations on variables and values. Java has 7 categories of operators — arithmetic, comparison, logical, assignment, bitwise, ternary, and unary.

1. Arithmetic Operators

int a = 10, b = 3;

System.out.println(a + b);   // 13 (addition)
System.out.println(a - b);   // 7  (subtraction)
System.out.println(a * b);   // 30 (multiplication)
System.out.println(a / b);   // 3  (integer division — truncates)
System.out.println(a % b);   // 1  (modulus — remainder)
⚠ Common Mistake

In Java, 10 / 3 = 3 (integer division), not 3.333. To get the decimal, make at least one operand a double: 10.0 / 3 = 3.333.

2. Unary Operators

int x = 5;

System.out.println(-x);     // -5 (unary minus)
System.out.println(+x);     // 5  (unary plus)

// Pre-increment: increment then use
int a = 5;
System.out.println(++a);    // 6

// Post-increment: use then increment
int b = 5;
System.out.println(b++);    // 5 (prints 5, b becomes 6)
System.out.println(b);      // 6

// Not operator
boolean flag = true;
System.out.println(!flag);  // false

3. Comparison (Relational) Operators

int x = 5, y = 10;

System.out.println(x == y);   // false (equal to)
System.out.println(x != y);   // true  (not equal)
System.out.println(x < y);    // true  (less than)
System.out.println(x > y);    // false (greater than)
System.out.println(x <= y);   // true  (less than or equal)
System.out.println(x >= y);   // false (greater than or equal)

4. Logical Operators

int age = 22;
boolean hasId = true;

System.out.println(age >= 18 && hasId);   // true (both must be true)
System.out.println(age < 18 || hasId);     // true (at least one true)
System.out.println(!hasId);                // false (negation)

Short-circuit behaviour

&& and || short-circuit — if the result is already known, the second condition is NOT evaluated.

int x = 5;
if (x > 10 && x++ > 0) {
    // x++ NOT executed because x > 10 is false
}
System.out.println(x);  // 5 (unchanged)

5. Assignment Operators

int x = 10;

x += 5;   // x = x + 5  → 15
x -= 3;   // x = x - 3  → 12
x *= 2;   // x = x * 2  → 24
x /= 4;   // x = x / 4  → 6
x %= 4;   // x = x % 4  → 2

System.out.println(x);

6. Ternary Operator

Short-hand for if-else. Syntax: condition ? valueIfTrue : valueIfFalse

int age = 20;
String status = (age >= 18) ? "Adult" : "Minor";
System.out.println(status);   // Adult

// Nested ternary
int marks = 85;
String grade = (marks >= 90) ? "A+" :
               (marks >= 80) ? "A" :
               (marks >= 70) ? "B" : "C";
System.out.println(grade);   // A

7. Bitwise Operators

Operate on individual bits. Rarely used but asked in interviews.

int a = 5;   // 0101
int b = 3;   // 0011

System.out.println(a & b);    // 1  (AND — 0001)
System.out.println(a | b);    // 7  (OR  — 0111)
System.out.println(a ^ b);    // 6  (XOR — 0110)
System.out.println(~a);       // -6 (NOT — inverts bits)
System.out.println(a << 1);   // 10 (left shift — multiply by 2)
System.out.println(a >> 1);   // 2  (right shift — divide by 2)

8. instanceof Operator

Checks if an object is an instance of a class or interface.

String name = "Ankit";
System.out.println(name instanceof String);   // true

Object obj = "Hello";
System.out.println(obj instanceof String);    // true

Operator Precedence

From highest to lowest:

  1. Postfix: x++, x--
  2. Prefix: ++x, --x, !
  3. Multiplicative: * / %
  4. Additive: + -
  5. Shift: << >>
  6. Relational: < > <= >= instanceof
  7. Equality: == !=
  8. Bitwise AND: &
  9. Bitwise XOR: ^
  10. Bitwise OR: |
  11. Logical AND: &&
  12. Logical OR: ||
  13. Ternary: ? :
  14. Assignment: = += -=

Real Example — Calculator

public class Calculator {
    public static void main(String[] args) {
        int a = 20, b = 6;

        System.out.println("Sum: " + (a + b));
        System.out.println("Difference: " + (a - b));
        System.out.println("Product: " + (a * b));
        System.out.println("Quotient: " + (a / b));
        System.out.println("Remainder: " + (a % b));

        // Ternary
        String bigger = (a > b) ? "a is bigger" : "b is bigger";
        System.out.println(bigger);
    }
}
💡 Academy Tip

Use == for primitive comparison but .equals() for String and object comparison. "hello" == "hello" can be false in some cases; "hello".equals("hello") is always correct.

📝 Try Yourself

Write a Java program that takes two numbers and prints: their sum, difference, product, integer quotient, real quotient (using double), and remainder. Then use ternary to print which is bigger. Also test pre vs post increment with a small program.

Quick Revision

  • Arithmetic: + - * / %.
  • Integer division truncates — use double for decimals.
  • Unary: ++ -- ! + -.
  • Comparison: == != < > <= >=.
  • Logical: && || ! — short-circuits.
  • Assignment: += -= *= /= %=.
  • Ternary: cond ? a : b.
  • Bitwise: & | ^ ~ << >>.
  • Use .equals() for String comparison.

Interview Questions

Q: Difference between && and & in Java?

A: && is logical AND and short-circuits — if the first condition is false, the second is not evaluated. & is bitwise AND and always evaluates both sides.

Q: Difference between pre-increment and post-increment?

A: ++x increments first, then returns the new value. x++ returns the current value, then increments. Example: if x=5, ++x gives 6 and x becomes 6. x++ gives 5, then x becomes 6.

Q: What is the difference between == and .equals()?

A: == compares references for objects (memory addresses). .equals() compares the actual content. For Strings, always use .equals().

Q: What is the ternary operator?

A: A one-line replacement for if-else: condition ? valueIfTrue : valueIfFalse. Example: int max = (a > b) ? a : b;

Q: Why does 10 / 3 give 3 instead of 3.333?

A: Because both operands are integers, Java performs integer division which truncates the decimal. Use 10.0 / 3 or cast one operand to double.

Want to Master Java?

Join our Java course in Varanasi — practical classes and career support at our Sarnath centre.

Ask on WhatsApp
Chat with us