Operators & Control Flow
java1-2 covered the eight primitives and the reference/value split. This chapter puts them to work — most of it will feel immediately familiar coming from C, C++, or Rust, with two genuine exceptions worth slowing down for.
Standard Operators
Arithmetic (+ - * / %), relational (== != < > <= >=), logical (&& || !), and compound assignment (+= -= *= /=) all behave exactly as they do in C/C++. Nothing new to learn here — genuinely shared syntax across all four languages this site has covered.
Integer Division & the Common C-Style Trap
Division between two int values truncates toward zero, exactly the same trap c1-3 covered for C. Java doesn't fix this — it's the same behavior, inherited from the same C-family lineage, and it requires the same explicit cast to force floating-point division.
Traditional switch — Fallthrough, the Same Footgun
Java's traditional switch falls through by default, identical to C's own switch statement covered in c1-3 — omitting break is a genuine, real bug class in both languages, not just a Java quirk.
Switch Expressions — Java's Real Fix
Java 14 introduced switch expressions: the arrow form (->) has no fallthrough at all — each branch is isolated — and the whole expression can produce a value directly, assigned straight into result. Multi-statement branches use a block with yield to supply the value.
Pattern Matching — A Light Touch
Since Java 16, instanceof can bind the checked-and-cast value directly to a new variable in one step, removing the old two-step check-then-cast pattern. Java 21 extends this into switch itself, matching directly on an object's type. This course won't go deep on pattern matching — it's flagged here mainly so the syntax isn't a surprise when it appears in later chapters.
| Behavior | C (c1-3) | Java traditional switch | Java switch expression |
|---|---|---|---|
| Fallthrough between cases | yes, by default | yes, by default | no — never falls through |
| Produces a value directly | no | no | yes, via -> or yield |
| Multiple labels per branch | stacked case labels | stacked case labels | comma-separated on one line |
switch fallthrough isn't a Java-specific gotcha — it's the identical bug class c1-3 covered for C's own switch, inherited unchanged. Missing one break silently executes the next case's body too.
Coding Challenges
Write a program that divides 7 by 2 using int division, then again using an explicit cast to force floating-point division. Print both results and label which is truncated.
📄 View solutionWrite a traditional switch statement over an int month (1-12) that deliberately omits a break in one case to demonstrate fallthrough, then rewrite the same logic as a switch expression using the arrow form and show it no longer falls through.
📄 View solutionWrite a method that accepts an Object and uses instanceof pattern matching to print its length if it's a String, or its value doubled if it's an Integer, with no explicit cast in either branch.
📄 View solutionChapter 3 Quick Reference
- Arithmetic/relational/logical/compound-assignment operators are shared syntax with C/C++
- int/int division truncates toward zero — identical to C's own behavior (c1-3), fixed the same way: cast one operand to double
- Traditional switch falls through by default — the same real bug class as C's switch
- Switch expressions (->) never fall through and can produce a value directly; yield supplies a value from a multi-statement block
- instanceof pattern matching binds the cast value directly, removing the old check-then-cast two-step
- Next chapter: classes, objects, and Java's four access levels