What is a Variable in Java? Types and Examples

🔹 1. What Is a Variable? A variable in Java is a name given to a memory location that stores a value. In simple terms — 🔹 A variable acts like a container or box that holds some data (like a number, text, or true/false value) which can be used and changed in the program. Example: int age = 21; Here: - int → data type (tells Java that the variable stores an integer value) - age → variable name (label for the memory location) - 21 → value stored in memory So, in memory it looks like: age → [ 21 ] 🔹2. Why We Use Variables Variables are used to: 1. Store information (like marks, name, price, etc.) 2. Reuse data multiple times in the program. 3. Make programs dynamic — we can change values during execution. 4. Perform calculations or make decisions based on stored data. 🔹3. Syntax of Variable Declaration : dataType variableName = value; 🔹Example : int number = 10; String name = "Vimala"; double salary = 25000.75; boolean isPassed = true; You can also declare first, assign later: int x; x = 20; 🔹4. Types of Variables in Java Java provides three main types of variables depending on where they are declared and how they behave. They are 1. Local Variables 2. Instance Variables 3. Static Variables 🔸 1. Local Variables - Declared inside a method, constructor, or block. - Created when the method is called and destroyed when it ends. - No default value (you must assign a value before using it). - Stored in the stack memory. 🔹Example : class Example {   void display() {     int a = 10; // Local variable     System.out.println(a);   } } 🔸 2. Instance Variables - Declared inside a class, but outside any method. - Each object gets its own copy of the variable. - Stored in heap memory. - Have default values. 🔹Example : class Student {   int id;     // Instance variable   String name;   // Instance variable } 🔹 When you create two objects: Student s1 = new Student(); Student s2 = new Student(); s1.id = 101; s2.id = 102; Each object (s1, s2) has its own copy of id. 🔸 3. Static Variables - Declared using the static keyword inside the class. - Shared by all objects of that class (only one copy exists). - Stored in the method area (part of memory). - Have default values. - Can be accessed using the class name directly. 🔹Example : class Student {   int id;   String name;   static String college = "Amrita Sai"; // Static variable } 🔹5. Memory Representation of Variables public class Example {   // Instance variable (Heap Memory)   int instanceVar = 10;   // Static variable (Method Area)   static int staticVar = 20;   public void method() {     // Local variable (Stack Memory)     int localVar = 30;     System.out.println("Local Var: " + localVar);     System.out.println("Instance Var: " + instanceVar);     System.out.println("Static Var: " + staticVar);   }   public static void main(String[] args) {     Example obj = new Example();     obj.method();   } #Java #Core java #Codegnan Anand Kumar Buddarapu Uppugundla Sairam Saketh Kallepu }

  • diagram

To view or add a comment, sign in

Explore content categories