Java Variables
A variable in Java is a named place to store a value. Programs use variables to remember information, calculate with it, update it, and print it later.
Every Java variable has a type, a name, and a value. The type tells Java what kind of data the variable can hold, such as a whole number, decimal number, character, or text.
Declaring Variables
To create a variable, write its type followed by its name. This is called declaring a variable. Most beginner programs also give the variable a starting value right away.
public class Main {
public static void main(String[] args) {
int apples = 6;
double price = 0.75;
char grade = 'A';
String course = "Java";
System.out.println("Apples: " + apples);
System.out.println("Price: $" + price);
System.out.println("Grade: " + grade);
System.out.println("Course: " + course);
apples = 8;
System.out.println("Updated apples: " + apples);
}
}
Output:
Apples: 6
Price: $0.75
Grade: A
Course: Java
Updated apples: 8
How It Works
The statement int apples = 6; creates a variable named apples. Its type is int, which stores whole numbers, and its starting value is 6.
The statement double price = 0.75; stores a number with a decimal point. The statement char grade = 'A'; stores one character. A char value uses single quotes.
The statement String course = "Java"; stores text. A String value uses double quotes. Notice that String begins with an uppercase S.
After a variable exists, you can use its name in output. In Java, the + operator can join text with a variable’s value, as in "Apples: " + apples.
Changing a Variable
You can assign a new value to an existing variable with the assignment operator, =. In the example, apples = 8; replaces the old value 6 with the new value 8.
Do not repeat the type when changing a variable that has already been declared. Write apples = 8;, not int apples = 8; in the same block of code.
Declaration and Initialization
Declaration means creating the variable name and type. Initialization means giving the variable its first value.
You can declare and initialize a variable in one statement:
int score = 10;
You can also declare first and assign later:
int score;
score = 10;
Both forms are valid, but initializing a variable right away is often clearer for beginners because the variable starts with a known value.
Common Variable Types
| Type | Stores | Example |
|---|---|---|
int |
Whole numbers | int age = 21; |
double |
Decimal numbers | double total = 19.99; |
char |
One character | char letter = 'B'; |
boolean |
true or false |
boolean passed = true; |
String |
Text | String name = "Maya"; |
Variable Names
A Java variable name can contain letters, digits, underscores, and dollar signs, but it cannot start with a digit. Variable names are case-sensitive, so score and Score are different names.
Choose names that explain what the value means. Names like itemCount, totalPrice, and studentName are easier to understand than names like x or n when a program grows.
Common Mistakes
- Forgetting the semicolon after a declaration or assignment.
- Using a variable before declaring it.
- Writing a variable name with different capitalization by accident.
- Putting text in single quotes instead of double quotes.
- Trying to store the wrong kind of value for the variable’s type.
Takeaway: declare a Java variable with a type and a name, initialize it with a useful starting value, and use assignment when the value needs to change.
