Skip to main content

Variables

Variables

A variable is a name given to a memory location. It is the basic unit of storage in a program.

  • The value stored in a variable can change during program execution.
  • A variable is just a name for a memory location — operations on the variable affect that memory location.
  • In Java, all variables must be declared before use.

Types of Variables in Java

Now let us discuss different types of variables which are listed as follows:

  • Primitive variables
  • Reference variables

Primitive Variables: With primitive data types a variable is a section of memory reserved for a value of that type.

For example, declaring a long reserves 64 bits for an integer value.

The eight primitives in Java are: int, byte, short, long, float, double, boolean, char.

Examples:

int x;
byte b;
long l;
float f;

Reference Variables: A reference variable stores the address/reference of an object (not the object itself).

Example:

Student s = new Student();

Here s is a reference variable that refers to a Student object.

Based on declaration position & behavior, variables are divided into: Local, Instance and Static variables.

Local Variables

Local variables are declared inside a method, constructor or block. They are stored on the stack and exist only while the block executes.

Key points:

  • Scope is the block where declared — destroyed when block completes.
  • Local variables are thread-safe.
  • JVM does not provide default values — local variables must be explicitly initialized before use.
  • The only modifier allowed for local variables is final. Using access modifiers (public/private/etc.) on local variables will cause a compile error.

Example 1 (shows compile error if uninitialized):

class Test {
  public static void main(String... args) {
    int x;
    System.out.print(x); // compile-time error: variable x might not have been initialized
  }
}

Example 2 (valid — variable unused but program prints):

class Test {
  public static void main(String... args) {
    int x;
    System.out.print("Hello"); // prints Hello
  }
}

Example 3 (still compilation error if not guaranteed initialized):

class Test {
  public static void main(String... args) {
    int x;
    if (args.length > 0) {
      x = 20;
    }
    System.out.print(x); // compile-time error: x might not have been initialized
  }
}

Only final is allowed as a modifier for local variables. Other modifiers like public/private/static on local variables are invalid.

example screenshot
Figure: example screenshot
example screenshot 2
Figure: another screenshot

Short summary

  • Primitive: stores actual value (int, long, double, etc.).
  • Reference: stores address to object (e.g. Student s = new Student()).
  • Local: declared inside methods/blocks — must be initialized explicitly.
  • Instance / Static: (covered in next section) — declared at class level and receive default values.

Popular posts from this blog

Java

Codes With Java — Basics Codes With Java Java tutorials & fundamentals About Contact Privacy Basic Fundamentals Java source file structure Import Statement Static Import Packages Data Type Variables Final Variable Declaration and Access Modifier Inner classes applicable modifiers Static Modifier Synchronized Native Transient Volatile Interface Introduction Interface Declaration and Implementation Interface methods and variables Naming Conflicts Interface Marker interface and Ad...

Short Circuite Operators part 4

                                                             Short Circuit Operators In  Java logical operators , if the evaluation of a logical expression exits in between before complete evaluation, then it is known as  Short-circuit . A short circuit happens because the result is clear even before the complete evaluation of the expression, and the result is returned. Short circuit evaluation avoids unnecessary work and leads to efficient processing. 1-: AND(&&) 2-:OR(||) these are exactly same as bitwise operators (&,|) except the following differences. Single Short Circuit Operator(&,|) Both arguments Should be evaluated always. relatively performance is low. Applicable for both boolean and Integral types. Double Short Circuit Operator(...

Operators & Assignment part 3

                                                                                       Operators & Assignment 1-:instanceof Operators 2-:Bitwise Operators                                                                        instanceof Operators we can use instanceof operator to check whether the given object is of a particular type are not. Example-: /* list is a collection of objects. List l=new List(); l.add(Customer); l.add(Student); l.add(Test); Object o=l.get(...