Java
Module 3: Java OperatorsBeginner22 min read

Arithmetic, Unary, Relational & Logical Operators

Understand all core Java operators, short-circuit evaluation with && and ||, and prefix vs postfix increment/decrement.

Key Takeaways & Core Concepts
  • Unary (`+`, `-`, `++`, `--`, `!`): ++x increments first and returns the new value; x++ returns the original value and then increments.
  • Relational (`==`, `!=`, `>`, `<`, `>=`, `<=`): Always evaluate to boolean.
  • Logical (`&&`, `||`, `!`): Use short-circuit evaluation: in a != null && a.length() > 0, if a != null is false, Java skips evaluating the right-hand side completely, preventing a NullPointerException.

Introduction

Operators are special symbols that compute values from one, two, or three operands. Mastering operator behavior—especially integer division, implicit casts in compound assignment, and short-circuiting—prevents subtle logic bugs in interviews and production code.

Arithmetic, Increment & Short-Circuit Guards
1public class Main {
2 public static void main(String[] args) {
3 int a = 7, b = 2;
4 System.out.println("Int division: " + (a / b) + ", Float: " + ((double) a / b));
5
6 int x = 5;
7 System.out.println("x++ = " + (x++) + ", after = " + x);
8 System.out.println("++x = " + (++x));
9
10 String text = null;
11 boolean hasText = (text != null && !text.isEmpty()); // Short-circuits safely
12 System.out.println("Has text? " + hasText);
13 }
14}
Click code or "Edit Code" to edit
Interactive Code Block•Tab = 4 spaces•Ctrl + Enter = Run
14 lines
Note: Casting at least one operand to `double` triggers floating-point division.

Learning Objectives

  • Apply arithmetic, unary, assignment, relational, and logical operators accurately.
  • Explain integer division vs floating-point division and avoid division-by-zero traps.
  • Leverage short-circuit evaluation (&&, ||) for safe null-checks.

Detailed Operator Theory

  • Arithmetic (`+`, `-`, `, /, %)**: Dividing two int values performs **integer division** (truncates toward zero: 7 / 2 == 3). Dividing an int by 0 throws ArithmeticException, whereas dividing a double by 0.0 yields Infinity or NaN`.
  • Unary (`+`, `-`, `++`, `--`, `!`): ++x increments first and returns the new value; x++ returns the original value and then increments.
  • Compound Assignment (`+=`, `-=`, `=, /=, %=)**: Automatically inserts an implicit narrowing cast! For example, int x = 5; x += 2.5; compiles and sets x = 7, whereas x = x + 2.5;` fails to compile.
  • Relational (`==`, `!=`, `>`, `<`, `>=`, `<=`): Always evaluate to boolean.
  • Logical (`&&`, `||`, `!`): Use short-circuit evaluation: in a != null && a.length() > 0, if a != null is false, Java skips evaluating the right-hand side completely, preventing a NullPointerException.

Common Mistakes

  • Using == to compare two String or Integer objects instead of .equals().
  • Writing if (x = 5) (invalid in Java because x = 5 evaluates to int, not boolean—unlike C!).
Practice & AssessmentJava Programming Tutorial

Test Your Understanding

Reinforce core concepts with interactive multiple-choice questions, live coding challenges, and top technical interview questions.

Your Current Score

0 / 0 correct(0 attempted)