๐ WAYPOINT LESSON
Declaring and assigning
โญ beginnerโณ 12 min read๐ Lesson 5 of 256
The primitive types, assignment semantics, boolean and char literals.
A variable is a typed storage box: you declare its type once, and Java enforces it forever.
int score = 95; // whole numbers
double average = 91.5; // decimals
boolean passed = true; // true or false
char letter = 'A'; // ONE character, single quotes
String name = "An"; // text, double quotes
Assignment replaces; it does not add. Trace this:
int x = 5;
x = 10; // x is 10 now โ the 5 is gone
x = x + 3; // right side evaluated first: 10 + 3, then stored
After those three lines, x is 13. The right side of = is always
evaluated before the store happens.
boolean holds exactly two values, true and false โ no numbers, no
"truthy" shortcuts like other languages. char uses single quotes
('A'), String uses double quotes ("A"). Mixing them is a classic
compile error.