Java OOP · Module 0
Variables, types and printing
The vocabulary every later lesson assumes
What you'll be able to do
- Declare a variable and explain why Java forces you to give it a type
- Tell the primitive types (int, double, boolean, char) apart from String
- Print values and join them together with +
- Read the skeleton of a Java program without being scared of it
Variables
A variable is a labelled box
Programs are mostly about storing values and moving them around. A variable is where a value waits.
A variable is a named place in memory that holds one value. In Java you must say *what kind* of value it will hold before you use it — that is called the type. Java checks types while compiling, which means many mistakes are caught before the program ever runs.
Boxes in a warehouse
Imagine labelled boxes: the box labelled age only accepts whole numbers, the box labelled price accepts decimals, and the box labelled name accepts text. If you try to drop a decimal into the whole-number box, the warehouse manager (the compiler) stops you at the door.
Four declarations, read one by one
int = whole number. age is the name. = 20 puts the first value in the box. The ; ends the statement.double stores decimals. Money, averages and measurements usually need double, not int.boolean holds only true or false — the type behind every if decision you will ever write.String holds text and is written with a capital S because it is not a primitive: it is an object type. Remember that word — Module 1 starts there.Naming matters more than you think
Java convention: variables and methods use camelCase (bankBalance), classes use PascalCase (BankAccount). Good names are the cheapest documentation you will ever write.
Data Types
Primitives, String, and the division trap
One of the most common beginner bugs in Java is not a typo — it is integer division.
The types you will actually use in this course
| Type | Stores | Example | Default value |
|---|---|---|---|
| int | whole numbers | 42 | 0 |
| double | decimal numbers | 3.5 | 0.0 |
| boolean | true / false | true | false |
| char | a single character | 'A' | '\u0000' |
| String | text (an object) | "Sara" | null |
The default-value column matters later: fields you never assign start with exactly these values.
Why this will bite you in Module 4
Bank balances are double. If you store money in an int, balance / 3 silently loses cents. Choosing the type *is* a design decision.
Joining values with +
When one side of + is a String, Java turns the other side into text and glues them: "Age: " + 20 produces Age: 20. When both sides are numbers, + is ordinary addition. This one rule explains most surprising output in beginner code.
Java Syntax
Every Java program starts in main
The skeleton you will type hundreds of times
public = callable from outside, static = belongs to the class not to an object, void = returns nothing, String[] args = command-line arguments.println = print line; print would stay on the same line.{ needs its }. Unbalanced braces are the number one compile error for beginners.About `static`
static means 'belongs to the class itself, not to an individual object'. That is why main can run before any object exists. Once you meet objects in Module 2, this sentence will suddenly make a lot more sense — come back and reread it.
Change `hours` to 9 and predict the new output before pressing Run.
Common mistakes
int average = 7 / 2;string name = "Sara";System.out.println("Total: " + 1 + 2);Lesson playground
Print a receipt line for any product. Try changing the types and watch the output change.
Check understanding
Quiz
Question 1 of 5
You are storing a bank balance that must keep cents. Which type do you choose?
Summary
- A variable = type + name + value, and Java checks the type at compile time.
- int, double, boolean and char are primitives; String is an object type (capital S).
- int / int stays int — cast to double when you need the fraction.
- `+` means addition between numbers and concatenation as soon as one side is text.