Java Expressions, Operators & Assignment
Launching JShell & Initial Setup
Open the Java REPL by typing
jshellin 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 * 5→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 * 10→ )Addition
+(e.g.,5 + 10→ )Subtraction
-(e.g.,5 - 10→ )Division
/(e.g.,10 / 2→ )Modulus
%(a.k.a. remainder)9 % 2→ (because )8 % 2→
Integer Literals & Data Type Context
Integer literal: whole number with no decimal part (…, ).
Arithmetic between two
intliterals yields anintresult (important for division—see later).Negative integers behave exactly like positive ones with respect to the five operators.
Practice Exercises (In-Video)
Minutes in a day
24 hours · 60 minutes/hour →
24 * 60⇒ minutes.
Seconds in a day
24 hours · 60 minutes/hour · 60 seconds/minute →
24 * 60 * 60⇒ 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 * 6→5 + 30⇒5 - 2 * 2→5 - 4⇒
Use parentheses
()to override/order evaluation and to improve readability:(5 - 2) * 2→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(xis 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 / 2⇒ (not )
To obtain a fractional result, at least one operand must be floating-point (
double):5.0 / 2or5 / 2.0or5.0 / 2.0⇒
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 1number--or--number⇒ subtract 1Minor 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 tox = 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!