A variable is a named storage location used to hold data in a Java program. Variables allow us to store information, perform calculations, and use values throughout our programs.
A variable is a named location that stores a value of a particular data type.
int age = 20;
System.out.println(age);
Output:
20
In this example:
int is the data type.age is the variable name.20 is the stored value.Variable declaration means specifying the variable's data type and name.
int age;
double price;
String name;
char grade;
boolean isPassed;
These statements declare variables without explicitly assigning values to them.
Local variables must be assigned a value before they are read. Fields receive default values automatically.
Initialization means assigning an initial value to a variable.
int age = 20;
double price = 99.50;
String name = "Rahul";
char grade = 'A';
boolean isPassed = true;
A variable can be declared and initialized in the same statement.
Example:
int marks = 85;
System.out.println(marks);
Output:
85
Java provides different data types for storing different kinds of information.
| Data Type | Example | Purpose |
|---|---|---|
int |
int age = 20; |
Whole numbers |
double |
double price = 99.5; |
Decimal numbers |
char |
char grade = 'A'; |
A single character |
boolean |
boolean active = true; |
True or false values |
String |
String name = "Rahul"; |
Text |
Note that String is a class, not a
primitive data type.
After declaring a variable, you can assign a new value to it if it is not declared as a constant.
int age = 20;
System.out.println(age);
age = 25;
System.out.println(age);
Output:
20
25
The second assignment replaces the previous value
stored in age.
You can declare multiple variables of the same type in one statement.
int a = 10, b = 20, c = 30;
System.out.println(a);
System.out.println(b);
System.out.println(c);
Output:
10
20
30
You can also declare variables separately when it makes your code easier to understand.
Java identifiers must follow specific naming rules.
Valid variable names:
int age = 20;
String studentName = "Rahul";
int total_marks = 450;
int number1 = 100;
int _count = 5;
Invalid variable names:
int 2number = 10;
int student-name = 20;
int class = 5;
A local variable is declared inside a method, constructor, or block. Its scope is limited to the relevant block.
public class Example {
public static void main(String[] args) {
int number = 100;
System.out.println(number);
}
}
Output:
100
Local variables must be definitely assigned before they are used.
An instance variable is declared inside a class but outside its methods and constructors. Each object has its own copy of instance variables.
public class Student {
String name;
int age;
public static void main(String[] args) {
Student s1 = new Student();
s1.name = "Rahul";
s1.age = 20;
System.out.println(s1.name);
System.out.println(s1.age);
}
}
Output:
Rahul
20
Instance variables receive default values if no explicit initial value is provided.
A static variable belongs to the class rather than to an individual object. It is shared by instances of that class.
public class Student {
static String institute = "Soopro Pathshala";
public static void main(String[] args) {
System.out.println(Student.institute);
}
}
Output:
Soopro Pathshala
The static keyword declares a class-level
field in this example.
The final keyword prevents a variable
from being assigned again after it has been initialized.
final double PI = 3.14159;
System.out.println(PI);
Output:
3.14159
This assignment is not allowed after initialization:
PI = 3.14; // Compilation error
By convention, constants are often named using uppercase letters and underscores.
Variables can store values used in arithmetic calculations.
public class Addition {
public static void main(String[] args) {
int a = 10;
int b = 20;
int sum = a + b;
System.out.println("First number: " + a);
System.out.println("Second number: " + b);
System.out.println("Sum: " + sum);
}
}
Output:
First number: 10
Second number: 20
Sum: 30
The expression a + b adds the two
integer values and stores the result in sum.
Create a program that stores a student's name, marks, and grade.
public class StudentDetails {
public static void main(String[] args) {
String name = "Rahul";
int marks = 85;
char grade = 'A';
System.out.println("Student Name: " + name);
System.out.println("Marks: " + marks);
System.out.println("Grade: " + grade);
}
}
Output:
Student Name: Rahul
Marks: 85
Grade: A
Key Points:
final keyword prevents reassignment.1. Which keyword declares an integer variable?
2. Which keyword prevents a variable from being reassigned?
3. What is the output of this code?
int a = 10;
int b = 20;
System.out.println(a + b);