Java Expressions, Operators & Assignment

Launching JShell & Initial Setup

  • Open the Java REPL by typing jshell in your terminal/command-prompt.

    • If you are unsure how, review the dedicated “Launching JShell” video (≈3–4 steps earlier).

  • A prompt such as jshell> _ signals readiness to accept Java statements.

Basic Arithmetic Expressions

  • Typing a standalone arithmetic expression makes JShell evaluate and immediately echo the result.

    • Example: 5 * 55×5=255 \times 5 = 25

    • JShell shows something like $1 ==> 25; ignore the dollar-index for now (it is an automatically generated variable holding the result).

  • Vocabulary

    • Expression: combination of operands + operator(s) that yields a value.

    • Operand/Literal: fixed value used in an expression (e.g., 5, 10).

    • Operator: symbol that tells Java what action to perform (e.g., *, +).

Operators & Operands (Integer Focus)

  • Supported arithmetic operators demonstrated so far:

    • Multiplication * (e.g., 5 * 105050)

    • Addition + (e.g., 5 + 101515)

    • Subtraction - (e.g., 5 - 105-5)

    • Division / (e.g., 10 / 255)

    • Modulus % (a.k.a. remainder)

    • 9 % 211 (because 92×4=19 - 2 \times 4 = 1)

    • 8 % 200

Integer Literals & Data Type Context

  • Integer literal: whole number with no decimal part (…, 2,1,0,1,2,-2, -1, 0, 1, 2,\dots).

  • Arithmetic between two int literals yields an int result (important for division—see later).

  • Negative integers behave exactly like positive ones with respect to the five operators.

Practice Exercises (In-Video)

  1. Minutes in a day

    • 24 hours · 60 minutes/hour → 24 * 6014401\,440 minutes.

  2. Seconds in a day

    • 24 hours · 60 minutes/hour · 60 seconds/minute → 24 * 60 * 608640086\,400 seconds.

  • JShell tip: use ↑ / ↓ arrows to recall previous commands.

Expression Complexity & Operator Precedence

  • Java follows standard precedence:

    • *, /, % (higher)

    • +, - (lower)

  • Without parentheses, higher-precedence operations execute first:

    • 5 + 5 * 65 + 303535

    • 5 - 2 * 25 - 411

  • Use parentheses () to override/order evaluation and to improve readability:

    • (5 - 2) * 266

    • Even if precedence already gives the desired answer, explicit brackets help future readers.

Dealing with Invalid Expressions & Errors

  • JShell (and Java) enforce strict syntax; unrecognized sequences trigger errors such as

    • Illegal start of expression

  • Examples of invalid inputs:

    • 5 x 6 (x is not a valid operator)

    • 5*/5 (bad operator sequence)

    • $5.5 ($ has no numeric meaning here)

  • Treat errors as feedback; experimenting with wrong inputs builds intuition about correct syntax.

Integer vs Floating-Point Division

  • When both operands are integers, Java performs integer division (truncates the decimal part):

    • 5 / 222 (not 2.52.5)

  • To obtain a fractional result, at least one operand must be floating-point (double):

    • 5.0 / 2 or 5 / 2.0 or 5.0 / 2.02.52.5

  • Rule of thumb: int ÷ int ⇒ int; if either side is floating-point ⇒ result is floating-point.

Assignment Operator (=) Basics

  • Syntax: variable = expression;

    • Evaluates the right-hand expression, copies the resulting value into the left-hand variable.

  • Not mathematical equality—think of it as an arrow: “take right value, store into left location.”

  • Left side must be a variable; right side may be a variable, literal, or any expression.

  • Example walkthrough:

  int i = 10;
  int j = 15;
  i = j;    // now i == 15, old 10 is lost
  i = j * 2; // i == 30 (15*2)
  i = i - i; // i == 0

Increment, Decrement, and Compound Assignment

  • Increment / Decrement shortcuts

    • number++ or ++number ⇒ add 1

    • number-- or --number ⇒ subtract 1

    • Minor semantic difference (pre- vs post-increment) appears inside larger expressions—covered later.

  • Compound assignment operators (shorthand when the same variable appears on both sides)

    • x += 2; is identical to x = x + 2;

    • x -= 1;x = x - 1;

    • x *= 5;x = x * 5;

    • x /= 4;x = x / 4;

    • x %= 2;x = x % 2;

  • Good for concise, readable code while avoiding repetition of the variable name.

Best-Practice & Learning Recommendations

  • Pause videos and actively type expressions in JShell to cement understanding.

  • Use parentheses generously for clarity even when precedence would “technically” suffice.

  • Practice converting everyday computations (e.g., time, currency) into Java expressions.

  • Embrace errors: they are instant feedback that steers you toward proper syntax.

  • Remember: early coursework is intentionally slow-paced with puzzles and exercises to build strong foundations—enjoy experimenting!