Learn Java variables step-by-step with beginner-friendly explanations. Understand data types, declaration vs initialization, naming rules, and practice exercises to master Java basics.
A variable is like a labeled container that stores data in memory so your program can use it later.
👉 Think of it as a box:
Example:
String name = "Alex";
Here:
String → type of dataname → variable label"Alex" → stored valueJava is a strongly typed language. This means you must tell Java exactly what type of data a variable will hold before you use it, and you cannot change that type later.
Start by teaching just these five essential types:
| Category | Data Type | What it stores | Example |
|---|---|---|---|
| Number | int |
Whole numbers (integers). | 10, -5, 0 |
| Number | double |
Numbers with decimals. | 3.14, -99.9 |
| Character | char |
A single letter, number, or symbol. Always uses single quotes. | 'A', '7', '?' |
| Logic | boolean |
Only two possible values: true or false. | true, false |
| Text | String |
A sequence of characters. Always uses double quotes. Note the capital ‘S’! | "Hello", "Java" |
Teacher’s Note: It is crucial to emphasize that
Stringstarts with a capital ‘S’ while the others are lowercase. This introduces the idea thatStringis a slightly different breed (a Reference type) than the basic Primitives, without needing to overcomplicate the explanation early on.
Students need to learn how to actually write this in code. Teach this as a two-step process that can be combined into one.
Step 1: Declaration (Building the empty box) You tell Java the type and the name.
int playerScore;
String playerName;
Step 2: Initialization (Putting the first value in the box)
You use the equals sign (=) to assign a value. Read = as “gets the value of” rather than “equals”.
playerScore = 100;
playerName = "Alex";
The Standard Way (Combined) Show them that programmers usually do both on the exact same line to save time:
int playerScore = 100;
String playerName = "Alex";
boolean isGameOver = false;
Finally, cover the rules for the “labels” (variable names). If they learn good habits now, they will avoid frustrating errors later.
player score will crash the program).1stPlayer is bad; player1 is good).myFavoriteColor, accountBalance, isRainyDay.public class HelloName {
public static void main(String[] args) {
// Variable storing text
String name = "Alex";
// Print message using variable
System.out.println("Hello, " + name + "!");
}
}
String name = "Alex";
This line:
name"Alex" inside it"Hello, " + name + "!"
The + operator joins text and variables together.
Result printed:
Hello, Alex!
System.out.println(...);
Displays text on the screen.
📌 Why types matter: They tell Java how much memory to use and what operations are allowed.
✔ Allowed
name
studentAge
totalMarks
❌ Not allowed
1name
student age
class
Tips
_, or $favoriteNumberThe following Java code is broken. There are five lines of code, and each one has exactly one mistake. Can you find and fix them?
1. int player Score = 100;
2. string heroName = "Zelda";
3. boolean hasMagicKey = "true";
4. char letterGrade = 'A+';
5. double 1stLapTime = 45.2;
Look at the real-world information below. Which Java data type (int, double, boolean, char, or String) would be the best choice to store each piece of info?
Write the exact Java code to create the following variables. Remember to combine declaration and initialization, and use proper camelCase naming conventions!
Buddy.5.true.45.5.| [Java Expressions: Types, Examples & Practice Questions | Complete Guide](expressions/) |