Java Variables for Beginners: From Definition to Usage, Even Zero-Basics Can Understand!

Have you ever wondered how numbers, text, and dates are “remembered” when we write programs? Actually, this all relies on an important concept—variables. Just like how we use a “piggy bank” to save pocket money, a variable can be thought of as a “data piggy bank” specifically for storing the data we need. And the content of this “piggy bank” can be changed at any time!

Why do we need variables?

Without variables, we’d have to rewrite numbers or text every time we use them. For example, if we want to record “I am 18 years old” and later change it to 20, we’d have to type the number again. But with variables, we just “store” the number in the variable, and then we can retrieve or modify the data using the variable name directly. It’s as convenient as adding or removing pocket money from a piggy bank!

Defining Variables: Labeling the “Piggy Bank”

In Java, defining a variable requires three parts: type, variable name, and initial value (optional). It’s like labeling a piggy bank to tell it, “This is the jar for storing age” or “This is the jar for storing names.”

1. Type: Telling the variable what to store

Java is a “strongly typed language,” so every variable must first specify a data type. For example:
- int: Integers (e.g., age, scores)
- double: Decimals (e.g., height, weight)
- String: Text (e.g., names, addresses)
- boolean: Boolean values (only true or false, e.g., “whether an exam is passed”)

2. Variable Name: Naming the variable

Variable names should be descriptive to make usage easier later. For example:
- Use age for age, name for names, and score for scores.
- For multi-word names, the camelCase convention is recommended (capitalize the first letter of the second word), e.g., studentAge (student age).

3. Initial Value: “Putting money into the piggy bank”

You can assign a value directly during definition (recommended to avoid “empty jars”) or define first and assign later. For example:

// Define an int variable 'age' with initial value 18
int age = 18;

// Define a String variable 'name' with initial value "Xiao Ming"
String name = "Xiao Ming";

// Define a double variable 'height' with initial value 1.75
double height = 1.75;

// Define a boolean variable 'isPass' with initial value true
boolean isPass = true;

Variable Naming Rules (Pitfall Avoidance Guide!)

Variable names aren’t arbitrary—they must follow these rules, or the code will throw errors:
- Cannot use Java keywords: Words like class, if, for (Java’s internal terms) cannot be used as variable names.
- Cannot start with a number: E.g., 123score is invalid. Variable names must start with a letter, _, or $.
- Only use valid characters: Letters (A-Z, a-z), digits (0-9), underscores (_), or dollar signs ($). No spaces or special characters.
- Variable names cannot repeat: In the same code block, two variables with the same name are not allowed (e.g., int age = 18; int age = 20; will throw an error).

Using Variables: Manipulating Data in the “Piggy Bank”

Once defined, variables can be read or modified. The most common ways are printing the variable’s value or reassigning it directly.

1. Printing Variables (Checking the content)

Use System.out.println() to print the variable’s value, like taking money out of the piggy bank to check how much is inside.

// Define variables
int age = 18;
String name = "Xiao Ming";

// Print variables
System.out.println("Name: " + name); // Output: Name: Xiao Ming
System.out.println("Age: " + age);   // Output: Age: 18

Note: The + operator in strings automatically converts variables to text (e.g., name is a string, so concatenation works directly).

2. Modifying Variables (Updating the piggy bank’s content)

Variable values can be changed anytime, like adding or removing money from a piggy bank.

int score = 85;
System.out.println("Initial score: " + score); // Output: Initial score: 85

score = 92; // Modify the score
System.out.println("Score after exam: " + score); // Output: Score after exam: 92

Quick Summary

Variables are the most basic “data containers” in Java. Key points:
1. Definition: Type + Variable name + Initial value (format: Type variableName = initialValue;).
2. Naming: Descriptive, no duplicates, and no keywords.
3. Usage: Print to view or assign to modify.

At first, concepts like “type” and “variable name” might feel abstract, but remember the “piggy bank” metaphor: each variable is a labeled jar with a type that can hold data. Once mastered, you can combine them like building blocks to create more complex functionality!

Try defining a few variables yourself, like storing your name, age, and height, then print them to see the result!

Xiaoye