Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

README.md

Variables

What is a Variable?

A variable is a name given to a memory location. It is used to store data that can be changed during the execution of a program.

Syntax >> datatype variableName = value;

int a = 10;

+-------------+
|   a = 10    |
+-------------+

| Code          | Meaning                                                                 |
| ------------- | ----------------------------------------------------------------------- |
| int a;        | DeclarationReserves memory for an integer variable named `a`.        |
| a = 10;       | AssignmentAssigns the value `10` to the variable `a`.                |
| int a = 10;   | InitializationDeclaration + Assignment in a single step.             |


intData type

aVariable name

10Value assigned

 Memory is allocated based on the data type (int gets 4 bytes)

Points to Remember

Variables focus on memory allocation.

The data type determines how much memory is required and what type of value the variable can store.

Variables allow us to access and manipulate stored data by referring to the variable name, not the memory address.

Key TakeAways:

What happens when you declare a variable?

The compiler reserves memory in RAM to hold a value of the specified data type.

The variable name acts like a label pointing to that memory location.

int age = 25;  // Declaration


   Variable             Memory (4 bytes)
+-----------+         
|   age  = 25   
+-----------+        


int tells Java to reserve 4 bytes in RAM.

The value 25 will be stored in that 4-byte memory block.

The variable name age acts as a pointer to the reserved memory where the data (like 25) is kept.