Monday, 21 September 2015

Java Tutorial : Java Variable Basics

Master Core Java Concepts!

Subscribe to Ram N Java for clear explanations, practical code examples, and essential Java tutorials!

Subscribe Now on YouTube

What Is a Variable in Java?

In Java, a variable is simply the name of a reserved area allotted in memory (RAM). When you declare and initialize a variable, the Java Virtual Machine allocates a memory slot to hold your value and assigns your chosen variable name to that memory location.

How Variables Work in Memory

Consider the following simple variable declarations:

int age = 30;
long salary = 50000;

Here is what happens behind the scenes during execution:

  • int age = 30; — Once executed, a reserved area is allocated in Random Access Memory (RAM). The value 30 is stored inside, and the reserved memory location is named age.
  • long salary = 50000; — A separate reserved area in RAM is created to store the value 50000, and the name assigned to that memory area is salary.

Key Takeaways

✔️ Definition: A variable is a named memory location used to store data temporarily during program execution.

✔️ Data Types Determine Allocation: The data type (like int or long) dictates how much memory is reserved.

✔️ Direct Reference: You refer to the stored data in your code using the variable name rather than raw memory addresses.


Watch More Java Tutorials:

No comments:

Post a Comment

Tutorials