What is a Variable?
A variable is a named memory location used to store data that can be modified during program execution. Each variable has a specific data type, which determines the kind of data it can hold and the operations that can be performed on it.Characteristics of Variables
Every variable in Java has a name, type, value, scope, and memory location. Here are the main characteristics:Name (Identifier) : The name used to identify the variable. It must follow Java naming rules.
- Type (Data Type): The type of a variable specifies the kind of data it can store.
- Size (Memory Allocation): Every variable has a fixed size, and it cannot store data beyond the limit defined by its data type.
- Value: The value is the actual data which is stored in the variable.
- Scope: Scope refers to the region of a program where a variable is accessible or can be used.
- Default Value: In Java, every instance variable gets a default value if it is not initialized.
Rules for Naming Variables
There are some protocols and rules that need to be followed while declaring a variable in Java.- A variable name must start with a letter, underscore (_) or dollar sign ($); it cannot begin with a digit. However, digits can be used after the first character.
- A variable name can contain letters, digits, underscores, and dollar signs; no other special characters like '@', '#', '%', etc. are allowed.
- Variable names cannot be Java keywords, reserved words such as int, class, if, while, etc., cannot be used as identifiers.
- Java is case-sensitive, so variable names are case-sensitive. This means 'age', 'Age', and 'AGE' are treated as different variables.
- Spaces are not allowed in variable names. Java follows the camelCase convention for naming variables. The first word starts with a lowercase letter, and each subsequent word starts with an uppercase letter. (e.g., totalMarks, studentAge).
Declaration and Initialization
Variable Declaration
Variable declaration means defining the variable with a data type and name, without assigning any value.Example:
int age;
String name;
- In Java local variables can be declared without assigning values but must be assigned before using.
- For instance, variables in Java provide default values; it’s not mandatory to assign them.
- For static variables, Java also provides default values.
Variable Initialization
Variable initialization in Java means assigning a value to a variable after it has been declared.Syntax:
Example:datatype variableName = value;
int age = 20;
String name = "Aman";
// Java Program to show variable
// declaration and initialization
public class Main
{
public static void main(String[] args)
{
// Variable
int age = 20;
// Variable
String name = "Aman";
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
Output:
Name: Aman
Age: 20
Types of Variables in Java
Here are some types of variables based on the scope and lifetime in Java:1. Local Variable
A local variable is a variable that is declared inside a method, constructor, or block and can be used only within that area.
- A local variable is accessible only within that method/block.
- A local variable must be initialized before use, it has no default value.
- The memory of a local variable is created when the method is called and destroyed after it ends.
// Java Program to show
// local variable
import java.util.*;
public class Main
{
public static void display()
{
// local variable
int x = 10;
// local variable
int y = 20;
int sum = x + y;
System.out.println("Sum = " + sum);
}
public static void main(String[] args)
{
display();
}
}
Output:
2. Instance Variable (Member Variable)Sum = 30
An instance variable is a variable that is declared inside a class but outside any method, constructor, or block.
- An instance variable has default values.
- Instance variables are accessible using object references.
- The lifetime of an instance variable is till the object exists.
// Java Program to show
// instance variable
class Student
{
// instance variable
String name;
// instance variable
int age;
void display()
{
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
public static void main(String[] args)
{
Student s1 = new Student();
s1.name = "Aman";
s1.age = 20;
s1.display();
}
}
Output:
3. Static VariableName: Aman
Age: 20
A static variable is a variable that is declared using the static keyword inside a class.
- Static variables have only one copy that is shared among all objects.
- Static variables get memory when the class is loaded into memory. (not when object is created)
- It is accessed using class name as well as object name.
- Static variables are stored in a special memory area called the Method Area (Class Area).
// Java Program to show
// static variable
class Student
{
// static variable
static String college = "GLA";
void display()
{
System.out.println("College: " + college);
}
// Driver code
public static void main(String[] args)
{
Student s1 = new Student();
s1.display();
// Change value of static variable
Student.college = "IIT";
s1.display();
}
}
Output:
College: GLA
College: IIT
Conclusion
Variables in Java are fundamental elements used to store and manage data in a program. They act as named memory locations that hold values which can be accessed and modified during execution. Every variable must be declared with a specific data type, ensuring type safety and proper memory usage. Initialization of variables is important to avoid errors and ensure meaningful output. Java provides different types of variables, such as local, instance, and static variables, each of which serves a specific purpose. Overall, variables play a crucial role in making Java programs dynamic, efficient, and easy to understand.Frequently Asked Questions
1. What is a variable in Java?2. Why do we need variables in Java?A variable is a named memory location used to store data that can be used and modified during program execution.
3. What are the types of variables in Java?Variables help store data temporarily, making programs dynamic and allowing values to change during execution.
4. Can we use a variable without initializing it?There are three main types, Local variables, Instance variables, and Static variables.
5. What are the rules for naming variables in Java?Local variables must be initialized before use, while instance and static variables get default values automatically.
Variable names must start with a letter, _, or $, cannot start with numbers, cannot be Java keywords, and should be meaningful.
0 Comments