JAVA-Variables - Notes By ShariqSP
Java Variables
Variables in Java are containers used to store data values. They play a fundamental role in programming by allowing the manipulation and storage of data during program execution.
Types of Variables in Java
There are three main types of variables in Java:
- Local Variables: Variables declared inside a method or a block. They are only accessible within the method or block in which they are declared.
- Instance Variables (Non-Static Variables): Variables declared inside a class but outside any method. They are initialized when an object of the class is created and have separate memory space for each instance of the class.
- Static Variables (Class Variables): Variables declared with the
static
keyword inside a class but outside any method with thestatic
modifier. They are initialized only once when the class is loaded into memory and shared among all instances of the class.
Syntax of Variable Declaration, Initialization, and Re-initialization
Variable Declaration: Declaring a variable involves specifying its data type and optionally assigning an initial value.
int age; // Declaration of an integer variable
String name; // Declaration of a string variable
Variable Initialization: Assigning an initial value to a variable at the time of declaration or later in the program.
int age = 25; // Initialization of an integer variable
String name = "John"; // Initialization of a string variable
Variable Re-initialization: Assigning a new value to an existing variable.
age = 30; // Re-initialization of the age variable
name = "Smith"; // Re-initialization of the name variable
Memory Allocation for Variables in Java
In Java, memory allocation for variables depends on their type and scope:
- Local Variables: Memory for local variables is allocated on the stack when a method is called and deallocated when the method returns.
- Instance Variables: Memory for instance variables is allocated on the heap when an object of the class is created and deallocated when the object is garbage collected.
- Static Variables: Memory for static variables is allocated in the method area of the JVM when the class is loaded into memory and deallocated when the class is unloaded.
Memory Management in Java:
Java provides automatic memory management through garbage collection. The Java Virtual Machine (JVM) is responsible for managing memory allocation and deallocation. Garbage collection frees up memory by removing objects that are no longer referenced, thus preventing memory leaks and manual memory management errors often found in other programming languages.
Memory Areas in Java
Java applications use different memory areas for storing various types of data during program execution. Understanding these memory areas is essential for efficient memory management and optimal performance of Java programs.
Heap Memory
Heap memory is the runtime data area in which objects are allocated. All objects in Java are stored in the heap, including instance variables and arrays. When an object is created using the new
keyword, memory is allocated from the heap to store the object's data and instance variables. Heap memory is shared among all threads of a Java application and is managed by the garbage collector.
Static Pool Area
The static pool area, also known as the method area or the permanent generation (PermGen), is a part of the heap memory used for storing class-level data such as static variables, constant pool, method and constructor code, and metadata about classes and methods. Static variables and constants are stored in this area, and they are initialized when the class is loaded into memory and destroyed when the class is unloaded.
Stack Area
The stack area is used for storing method-specific variables and method call information. Each thread in a Java application has its own stack, which stores local variables, method parameters, and partial results of method invocations. Stack memory is allocated per thread and is used for method execution and method call stack frames. Variables declared within a method, including method parameters and local variables, are stored in the stack. Stack memory is automatically managed and is released when the method execution completes or the thread terminates.
Methods Area
The methods area, also known as the code cache, is a part of the runtime data area used for storing bytecode of loaded classes and their corresponding method code. It contains static variables and metadata about classes and methods, such as method signatures, field descriptors, and access flags. The methods area is shared among all threads of a Java application and is managed by the JVM. It is used for class loading, bytecode verification, and dynamic class generation.