Skip to content

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.

java
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):

TypeSizeExampleRange
int4 bytes42±2.1 billion
long8 bytes100L±9 quintillion
double8 bytes3.14~15 decimal digits
float4 bytes3.14f~7 decimal digits
boolean1 bittrue / false
char2 bytes'A'Unicode character

Reference types (stored as a pointer to an object):

java
String greeting = "Hello";  // String is a class, not a primitive
int[] numbers = new int[5]; // Arrays are objects

Key insight: Primitives hold values directly. Reference types hold an address pointing to where the actual data lives in memory.

java
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 = 5 then x = "hello" is fine.

C++ window: C++ has the same primitives plus unsigned variants and pointer types. But references behave differently — C++ has both value semantics (copies) and reference semantics.

Naming Conventions

ConventionApplies toExample
camelCasevariables, methodsuserName calculateTotal()
PascalCaseclasses, interfacesHelloWorld BankAccount
UPPER_SNAKE_CASEconstantsMAX_SIZE DEFAULT_TIMEOUT
kebab-casenot valid in Javauser-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

  1. Declare a variable of each primitive type and print it
  2. Try assigning a large value to a small type (byte b = 200;) — what happens?
  3. Declare two String variables, 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.

Built with VitePress | Software Systems Atlas