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 (`+`, `-`, `++`, `--`, `!`):
++xincrements 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, ifa != nullisfalse, Java skips evaluating the right-hand side completely, preventing aNullPointerException.
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 twointvalues performs **integer division** (truncates toward zero:7 / 2 == 3). Dividing anintby0throwsArithmeticException, whereas dividing adoubleby0.0yieldsInfinityorNaN`. - Unary (`+`, `-`, `++`, `--`, `!`):
++xincrements 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 setsx = 7, whereasx = x + 2.5;` fails to compile. - Relational (`==`, `!=`, `>`, `<`, `>=`, `<=`): Always evaluate to
boolean. - Logical (`&&`, `||`, `!`): Use short-circuit evaluation: in
a != null && a.length() > 0, ifa != nullisfalse, Java skips evaluating the right-hand side completely, preventing aNullPointerException.
Common Mistakes
- Using
==to compare twoStringorIntegerobjects instead of.equals(). - Writing
if (x = 5)(invalid in Java becausex = 5evaluates toint, notboolean—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)