Java Variables

Variables are one of the most basic and important concepts in Java. They are used to store data that a program needs to work with while it is running.

For example, a program may need to store a student’s age, name, marks, salary, or the result of a calculation. Java variables provide a way to give names to these values so that the program can access and use them.

What Is a Variable?

A variable is a named storage location that holds a value of a particular data type.

For example:

int age = 25;

In this statement:

  • int is the data type.
  • age is the variable name.
  • 25 is the value.
  • = is the assignment operator.

The variable can then be used elsewhere in the program:

System.out.println(age);

Output:

25

The name age allows the program to refer to the stored value.

Why Do We Need Variables?

Variables allow programs to store information and use that information later.

For example:

int price = 100;
int quantity = 3;

int total = price * quantity;

System.out.println(total);

Output:

300

Here, price, quantity, and total are variables.

Without variables, it would be difficult to store intermediate results or work with changing data.

Variables Can Hold Different Types of Data

Java provides different data types for different kinds of values.

For example:

int age = 25;
double salary = 45000.50;
char grade = 'A';
boolean isActive = true;
String name = "Karim";

Each variable has a type that determines what kind of value it can store.

Declaring Variables

Variable declaration means telling Java that you want to create a variable of a particular type and giving that variable a name.

The basic syntax is:

dataType variableName;

For example:

int age;

Here, Java is told that a variable named age should have the int type.

At this point, the variable has been declared, but no value has been explicitly assigned to it.

Another example:

double salary;
String name;
boolean isActive;

These statements declare three variables with different types.

Declaration With Multiple Variables

Java also allows multiple variables of the same type to be declared in one statement:

int age, marks, year;

However, declaring related variables separately is often easier to read:

int age;
int marks;
int year;

Declaration of Different Data Types

Variables can be declared using different Java data types:

int age;
double price;
char grade;
boolean passed;
String name;

Each variable has its own data type.

For example:

int age;

can store integer values, while:

double price;

can store decimal values.

Declaration Does Not Mean Initialization

It is important to understand that declaring a variable and giving it a value are two different things.

For example:

int age;

This is a declaration.

But:

int age = 25;

contains both declaration and initialization.

This distinction becomes particularly important when learning Java’s rules for local variables.

Initializing Variables

Initialization means giving a variable its first value.

For example:

int age = 25;

The variable age is declared as an int and initialized with the value 25.

Another example:

String name = "Karim";

Here, name is initialized with the string "Karim".

You can initialize variables of different types:

int age = 25;
double salary = 45000.50;
char grade = 'A';
boolean passed = true;
String name = "Karim";

Declaration and Initialization Together

The most common way to create a variable is to declare and initialize it in the same statement:

int age = 25;

This can be understood as:

Declaration → int age
Initialization → age gets 25

Another example:

double price = 99.99;

Here:

Declaration → double price
Initialization → price gets 99.99

Initialization After Declaration

A variable can also be declared first and initialized later.

For example:

int age;

age = 25;

The first statement declares the variable.

The second statement gives it its first value.

This is valid for a local variable as long as the variable is assigned a value before it is read.

For example:

int age;

age = 25;

System.out.println(age);

Output:

25

However, this is not valid:

int age;

System.out.println(age);

A local variable must be definitely assigned before it is used.

Initializing Different Variables

You can initialize several variables separately:

int age = 25;
int marks = 85;
double percentage = 85.5;

You can then use them throughout the appropriate scope:

System.out.println(age);
System.out.println(marks);
System.out.println(percentage);

Assigning Values

Assignment means giving a value to a variable using the assignment operator =.

For example:

int age;
age = 25;

The statement:

age = 25;

assigns the value 25 to the variable age.

The assignment operator is:

=

It should not be confused with the equality comparison operator:

==

For example:

age = 25;

means that 25 is assigned to age.

Whereas:

age == 25

checks whether age is equal to 25.

Assigning a New Value

A variable’s value can usually be changed after it has been initialized.

For example:

int age = 25;

age = 26;

System.out.println(age);

Output:

26

Initially:

age → 25

After:

age = 26;

the variable contains:

age → 26

The previous value is replaced by the new value.

Assigning Values Multiple Times

A variable can be assigned different values during program execution.

int score = 50;

score = 75;
score = 90;
score = 100;

System.out.println(score);

Output:

100

The final value of score is 100.

This ability to change values is one reason variables are useful in programming.

Assigning One Variable to Another

The value of one variable can be assigned to another variable.

For example:

int firstNumber = 10;
int secondNumber = firstNumber;

System.out.println(secondNumber);

Output:

10

Here:

int secondNumber = firstNumber;

means that the current value of firstNumber is assigned to secondNumber.

If the variables are primitive types, the value is copied.

For example:

int a = 10;
int b = a;

a = 20;

System.out.println(a);
System.out.println(b);

Output:

20
10

Changing a later does not change b because b received its own copy of the primitive value.

Assigning the Result of an Expression

A variable can also receive the result of a calculation.

For example:

int price = 100;
int quantity = 5;

int total = price * quantity;

System.out.println(total);

Output:

500

Here:

price * quantity

is evaluated first, and the resulting value is assigned to total.

Another example:

int a = 20;
int b = 10;

int result = a + b;

The value 30 is assigned to result.

Reassigning a Variable

When a new value is assigned to an existing variable, it is called reassignment.

int age = 20;

age = 21;

The variable was first initialized with 20 and then reassigned to 21.

You can also use the current value in a new assignment:

int count = 10;

count = count + 1;

System.out.println(count);

Output:

11

Java first evaluates:

count + 1

which produces 11, and then assigns that value back to count.

This is commonly written using the increment operator:

count++;

Difference Between Declaration, Initialization, and Assignment

These three terms are closely related but have different meanings.

ConceptExampleMeaning
Declarationint age;Creates a variable declaration with a type and name
Initializationint age = 25;Gives the variable its first value
Assignmentage = 30;Gives a value to an already declared variable
Reassignmentage = 35;Changes the value currently stored in the variable

Consider this example:

int age;

This is a declaration.

Then:

age = 25;

This is the variable’s first assignment, and it also initializes the variable.

Later:

age = 30;

This is reassignment because age already had a value.

The most common combined form is:

int age = 25;

This performs declaration and initialization in one statement.

Frequently Asked Questions

What is a variable in Java?

A variable is a named storage location used to hold a value of a particular type during program execution.

What is variable declaration?

Variable declaration tells Java the variable’s type and name.

int age;

Here, age is declared as an int.

What is variable initialization?

Initialization means giving a variable its first value.

int age = 25;

Here, age is initialized with 25.

What is assignment in Java?

Assignment means giving a value to a variable using the = operator.

age = 30;

Can I change the value of a variable?

Yes. Unless the variable has restrictions such as final, its value can normally be changed.

int age = 25;
age = 26;

What is the difference between = and ==?

= is the assignment operator.

age = 25;

== is used to compare values.

age == 25

Can I declare a variable without initializing it?

Yes, but the rules depend on where the variable is declared. A local variable can be declared without an initial value, but it must be definitely assigned before it is read.

int age;
age = 25;

System.out.println(age);

Can I declare and initialize a variable in one statement?

Yes.

int age = 25;

This is the most common form.

Can one variable be assigned to another?

Yes.

int a = 10;
int b = a;

The current value of a is assigned to b.

Can a variable store different values at different times?

Yes.

int score = 50;

score = 75;
score = 100;

The variable can hold different values at different points during program execution.

What happens when I assign a new value to a variable?

The variable’s previous value is replaced by the new value, provided the assignment is valid for that variable’s type.

int age = 20;

age = 25;

The current value of age becomes 25.