The Plain Picture#
A row of labelled boxes. Each box has a name written on it ("score"), holds one value at a time (200), and — in Java — is made for one kind of thing: a box for whole numbers can't hold text, just as an egg carton can't hold soup.
The Real Names#
A variable is a named box in memory that holds a value.
Its type says what kind of value it may hold, and therefore how many bytes it needs and how those bytes are read (How Computers Work Ch 4: bytes + an agreement).
Declaring a variable creates the box:
int score;. Assigning puts a value in:score = 200;. Both at once:int score = 200;(initialising).Java is statically typed: every variable's type is fixed when you write the code, and the compiler checks every use.
Under the Hood#
The 8 primitive types#
A primitive type holds a simple value directly in the box (everything else in Java is an object, reached by a reference — Classes, Objects and Memory).
| Type | Size | Holds | Range / notes | Example |
|---|---|---|---|---|
byte | 8 bits | whole number | −128 to 127 | byte b = 100; |
short | 16 bits | whole number | −32,768 to 32,767 | rarely used |
int | 32 bits | whole number | about ±2.1 billion — the default choice | int score = 200; |
long | 64 bits | whole number | about ±9.2 × 10¹⁸ | long views = 9_000_000_000L; |
float | 32 bits | decimal | ~7 significant digits | float f = 3.14f; |
double | 64 bits | decimal | ~15–16 significant digits — the default for decimals | double avg = 86.5; |
boolean | (JVM-dependent) | true or false | nothing else — not 0/1 | boolean onTime = true; |
char | 16 bits | one character | a UTF-16 unit, 0 to 65,535 | char grade = 'A'; |
All the sizes and ranges come straight from How Computers Work Ch 4. Real limits:
System.out.println(Integer.MAX_VALUE + " " + Long.MAX_VALUE);
// 2147483647 9223372036854775807Literals — how you write values#
int a = 200; // whole number literal → int
long b = 9_000_000_000L; // L suffix = long. Without it, this number is too big for an int → compile error
int million = 1_000_000; // underscores are ignored — just for readability
double c = 86.5; // decimal literal → double
float d = 86.5f; // f suffix = float
char e = 'A'; // single quotes: ONE character
String f = "A"; // double quotes: text (a String — Ch 4). 'A' and "A" are different types!
boolean g = true;
int h = 0xFF; // hex literal = 255 (01_how_computers_work.md Ch 3)
int k = 0b1100_1000; // binary literal = 200Naming rules and conventions#
Names may use letters, digits,
_and$, but can't start with a digit, and can't be a keyword (int,class,for...).Conventions everyone follows:
variables and methods: camelCase —
correctAnswers,bestScore;classes: PascalCase —
StudyBuddyReport;constants: UPPER_SNAKE_CASE —
POINTS_PER_CORRECT.
Name for meaning:
correctAnswers, notcaorx. In interviews, clear names are part of what's graded.
Constants — final#
final int POINTS_PER_CORRECT = 10;
POINTS_PER_CORRECT = 20; // compile error: cannot assign a value to final variablefinal means "this box can be filled once and never changed". Use named constants instead of unexplained numbers in code
(a 10 in the middle of a formula is a "magic number" — nobody knows what it means).
Default values and "definitely assigned"#
Java refuses to let you read a local variable (one declared inside a method) before you've assigned it:
int total;
System.out.println(total); // compile error: variable total might not have been initializedThis is a safety feature: in some languages the box would contain leftover garbage bytes from earlier.
Gotchas
intoverflow is silent (How Computers Work Ch 4.1). If a sum might pass ~2.1 billion — adding up many scores, multiplying two large numbers — uselong. This is one of the most common wrong-answer causes on LeetCode.charis a number underneath, so'A' + 1is66, not"B"(Ch 3).Don't use
floatunless you have a reason (memory-heavy arrays).doubleis the normal decimal type — and never for money (How Computers Work Ch 4.2).
Check yourself
5 questionsWhat's the difference between declaring and initialising a variable?
Show answerHide answer
Declaring creates the variable with its type (int score;); initialising also gives it its first value (int score = 200;).
Which type would you use for: a quiz score; the number of video views worldwide; an average; whether a quiz is finished; a grade letter?
Show answerHide answer
int; long; double; boolean; char.
Why does long x = 9000000000; fail to compile?
Show answerHide answer
A literal without L is an int, and 9 billion doesn't fit in an int. Write 9000000000L.
What does final do?
Show answerHide answer
Makes a variable assignable only once — a constant.
What are the naming conventions for variables, classes and constants?
Show answerHide answer
camelCase for variables/methods, PascalCase for classes, UPPER_SNAKE_CASE for constants.