01 / 21

Variable Declaration
& Assignment

Learn how to declare variables with a type and assign values in Java: the fundamental building block of every program.

Java

What Is a Variable?

A variable is a named container in your computer's memory that stores a single piece of data. Think of it as a labeled box: the label is the variable's name, and what you put inside is the value.

score 95 name value

Why Variables Matter

Store Data
Keep user input, calculation results, and configuration values for later use.
Reuse Values
Reference the same data many times without repeating yourself.
Change State
Update a score, toggle a flag, or track progress as a program runs.

Java is Statically Typed

In Java, every variable must be declared with a specific data type before it can be used. The compiler checks types at compile time, not at runtime.

int
Whole numbers: 42, -7, 0
double
Decimal numbers: 3.14, -0.5
boolean
True/false: true, false
String
Text: "Hello", "Java"

The Declaration Rule

Every variable declaration in Java follows a strict two-part syntax: the type comes first, then the name.

int type score name ; semicolon
int score; // declaration only int score = 95; // declaration + assignment

Declaration vs Assignment

Declaration Assignment
Purpose Creates a new variable Stores a value in an existing variable
Syntax type name; name = value;
Frequency Once per variable (per scope) As many times as needed
Example int age; age = 17;

Primitive Data Types

Type Size Range / Values Example
int 32 bits \( -2^{31} \) to \( 2^{31}-1 \) 42
double 64 bits Decimal precision 3.14
boolean JVM specific true or false true
char 16 bits Single character 'A'

Step-by-Step Approach

1
Choose the type. What kind of data will the variable hold? (int, double, boolean, String, etc.)
2
Pick a meaningful name. Use camelCase: studentAge, totalScore, isReady.
3
Write the declaration. Type first, then name: double gpa;
4
Assign a value. Use the = operator: gpa = 3.85;
5
End with a semicolon. Every statement in Java must end with ;

Variable Naming Rules

1
Must start with a letter, underscore (_), or dollar sign ($): never a digit.
2
Case-sensitive: score and Score are different variables.
3
No spaces or special characters (except _ and $).
4
Cannot be a reserved word: int, class, public, etc.

Example 1: Integers

// Declare and initialize an int int lives = 3; // Use the variable System.out.println(lives); // prints 3 // Re-assign (no type needed) lives = 2; System.out.println(lives); // prints 2

Example 2: Doubles & Strings

double price = 9.99; String item = "Notebook"; System.out.println(item + " costs $" + price); // Output: Notebook costs $9.99
double
Stores decimal numbers. Use for money, measurements, grades.
String
Stores text in double quotes. Capital S: it's a class, not a primitive.

Example 3: Booleans & Char

boolean isLoggedIn = false; char grade = 'A'; // Update the boolean isLoggedIn = true; System.out.println("Grade: " + grade); // Output: Grade: A System.out.println("Logged in: " + isLoggedIn); // Output: Logged in: true

How Memory Works

MEMORY int lives 3 double price 9.99 String item "Notebook" boolean isLoggedIn true

Constants with final

Adding the keyword final before a declaration makes the variable a constant: once assigned, its value can never change.

final double PI = 3.14159; final int MAX_SCORE = 100; // This would cause a compile error: // PI = 3.14; ← ERROR: cannot reassign final

Common Mistakes

✕ Missing Type
Writing score = 10; without declaring the type first. Java requires: int score = 10;
✕ Re-declaring
Writing int score = 10; then int score = 20; in the same scope. Drop the type on reassignment.
✕ Type Mismatch
Assigning "hello" to an int variable. The value must match the declared type.
✕ Missing Semicolon
Forgetting the ; at the end. Java treats it as an incomplete statement and throws an error.

Summary Cheat Sheet

Action Syntax Example
Declare type name; int age;
Assign name = value; age = 17;
Initialize type name = value; int age = 17;
Reassign name = newValue; age = 18;
Constant final type NAME = value; final int MAX = 100;

Question 1 of 5

Which of the following correctly declares and initializes an integer variable named count with the value 10?

Question 2 of 5

What happens when you try to compile this code?
double temperature;
System.out.println(temperature);

Question 3 of 5

Which variable name is valid in Java?

Question 4 of 5

What is the value of x after these lines run?
int x = 5;
x = x + 3;
x = 1;

Question 5 of 5

Which line will cause a compile error?