Metadata Card
- Prerequisites: Ch1 (First Program)
- Estimated time: 40 min
- Core difficulty:
- Reading mode: Easy stroll
- Completion marker: Can declare variables of different types, understand type conversion, scope, and naming conventions
Your Progress
You said hello to the machine. Now it's time to teach it your name, your age, your balance — the data that makes your program do something useful. Variables are the memory cells where programs keep their data.
Your Task
You need to store information: a user's name (String), their age (int), their account balance (double). You need to name these storage locations, assign values to them, and use those values later. This chapter covers exactly that.
The Breakthrough · Tracing the Origins
Act 1: The Variable Workshop
In the workshop, you have labeled shelves. Each shelf holds a specific type of material — iron ingots on one shelf, copper wire on another. A variable is like a labeled shelf in the computer's memory.
int age = 25;
double price = 99.99;
String name = "Alice";
boolean isActive = true;Language: Java How to run: Put these in a main method and run What happened: You told the JVM: "set aside a block of memory, call it age, and put the value 25 in it"
Type = What kind of data. Java is statically typed — every variable has a declared type, checked at compile time.
Types in Java
Primitive types (stored directly in memory — fast, no object overhead):
| Type | Size | Example | Range |
|---|---|---|---|
int | 4 bytes | 42 | ±2.1 billion |
long | 8 bytes | 100L | ±9 quintillion |
double | 8 bytes | 3.14 | ~15 decimal digits |
float | 4 bytes | 3.14f | ~7 decimal digits |
boolean | 1 bit | true / false | — |
char | 2 bytes | 'A' | Unicode character |
Reference types (stored as a pointer to an object):
String greeting = "Hello"; // String is a class, not a primitive
int[] numbers = new int[5]; // Arrays are objectsKey insight: Primitives hold values directly. Reference types hold an address pointing to where the actual data lives in memory.
int a = 5;
int b = a; // b gets a *copy* of 5
String x = "hello";
String y = x; // y gets a *copy of the reference* — both point to the same "hello"Python window: Python doesn't have primitives — everything is an object. Variables are dynamically typed:
x = 5thenx = "hello"is fine.C++ window: C++ has the same primitives plus
unsignedvariants and pointer types. But references behave differently — C++ has both value semantics (copies) and reference semantics.
Naming Conventions
| Convention | Applies to | Example |
|---|---|---|
| camelCase | variables, methods | userName calculateTotal() |
| PascalCase | classes, interfaces | HelloWorld BankAccount |
| UPPER_SNAKE_CASE | constants | MAX_SIZE DEFAULT_TIMEOUT |
| kebab-case | not valid in Java | user-name (invalid!) |
Your task: After reading this chapter, you should be able to:
- Declare variables of all primitive types
- Understand the difference between primitives and references
- Follow Java naming conventions
- Understand type casting (widening and narrowing)
Final Challenge
- Declare a variable of each primitive type and print it
- Try assigning a large value to a small type (
byte b = 200;) — what happens? - Declare two
Stringvariables, assign them the same literal value, and compare with==vs.equals()
Traveler's Notes
Variables are labeled storage boxes in memory.
Type defines what kind of data the box can hold.
Primitives hold values directly; references point to objects.
Naming conventions keep code readable — camelCase for variables and methods, PascalCase for classes.
Java checks types at compile time — it won't let you put a square peg in a round hole.