What are Variables in Java?
In Java, variables are containers that hold data values during program execution. A variable provides a way to store, retrieve, and manipulate data dynamically. Every variable in Java must have a type that determines the kind of data it can hold, such as numbers, characters or boolean values.
Why are Variables Important?
Variables are essential because they:
- Enable dynamic data handling during program execution.
- Help developers create flexible and reusable code.
- Simplify complex computations by storing intermediate results.
Types of Variables in Java
Java supports three main types of variables:
1) Local Variables
- Declared inside a method, constructor, or block.Accessible only within the scope where it is declared.Must be initialized before use.
public class LocalVariableExample {
public static void main(String[] args) {
int number = 10; // Local variable
System.out.println("The number is: " + number);
}
}
Output:
The number is: 10
2) Instance Variables
- Declared inside a class but outside any method, constructor or block. Belongs to an object of the class and has default values (e.g., 0 for integers, null for objects). Can be accessed using an object of the class.
Example:
public class InstanceVariableExample {
String name; // Instance variable
public static void main(String[] args) {
InstanceVariableExample obj = new InstanceVariableExample();
obj.name = "Java";
System.out.println("Language: " + obj.name);
}
}
Output:
Language: Java
3) Static Variables (Class Variables)
- Declared with the static keyword inside a class but outside any method or block. Shared among all objects of the class. Useful for storing common properties of objects.
Example:
public class StaticVariableExample {
static String platform = "Java Programming"; // Static variable
public static void main(String[] args) {
System.out.println("Platform: " + platform);
}
}
Output:
Platform: Java Programming
Declaring Variables in Java
To declare a variable in Java, you must specify its type followed by its name. Optionally, you can initialize it during declaration.
Syntax:
type variableName = value;
Example:
int age = 25; // Declaring and initializing an integer variable
Rules for Naming Variables
- Variable names must start with a letter or $ or _ but cannot begin with a digit.
- Variable names are case-sensitive (e.g., Age and age are different).
- Avoid using reserved keywords as variable names.
Valid Names: myVar, _count, $value
Invalid Names: 123abc, class
Primitive Data Types for Variables
Java provides eight primitive data types for variables:
- byte: 1 byte, stores integers (-128 to 127).
- short: 2 bytes, stores integers (-32,768 to 32,767).
- int: 4 bytes, stores integers (-2^31 to 2^31-1).
- long: 8 bytes, stores large integers (-2^63 to 2^63-1).
- float: 4 bytes, stores decimal numbers.
- double: 8 bytes, stores large decimal numbers.
- char: 2 bytes, stores a single character.
- boolean: 1 bit, stores true or false.
Example of Multiple Data Types:
public class DataTypeExample {
public static void main(String[] args) {
int age = 30;
double salary = 45000.50;
char grade = 'A';
boolean isActive = true;
System.out.println("Age: " + age);
System.out.println("Salary: " + salary);
System.out.println("Grade: " + grade);
System.out.println("Is Active: " + isActive);
}
}
Output:
Age: 30
Salary: 45000.5
Grade: A
Is Active: true
Variable Scope
The scope of a variable determines where it can be accessed in the code.
- Local Scope: Limited to the method or block where it is declared.
- Instance Scope: Accessible within the class but requires an object to use it.
- Class (Static) Scope: Accessible using the class name without creating an object.
Example:
public class VariableScopeExample {
int instanceVariable = 100; // Instance variable
static int staticVariable = 200; // Static variable
public static void main(String[] args) {
int localVariable = 50; // Local variable
System.out.println("Local Variable: " + localVariable);
VariableScopeExample obj = new VariableScopeExample();
System.out.println("Instance Variable: " + obj.instanceVariable);
System.out.println("Static Variable: " + staticVariable);
}
}
Output:
Local Variable: 50
Instance Variable: 100
Static Variable: 200
Final Variables
The final keyword makes a variable constant, meaning its value cannot be changed once assigned.
Example:
public class FinalVariableExample {
public static void main(String[] args) {
final int MAX_AGE = 100;
System.out.println("Max Age: " + MAX_AGE);
// MAX_AGE = 110; // This will cause a compilation error
}
}