Variable in Java
Variables and Constants:
Before talking about other tokens, let us understand what exactly are constants and variable in java:
Let’s do this with a practical example from a real-life situation:
Suppose you have a few biscuits and a container. You are asked to store the biscuits in the container, which can be represented as:
Container = Biscuits [This means the container location has biscuits]
Another situation that might happen:
You are asked to serve the biscuits on a plate, which can be represented as:
Plate = Biscuits
I.e. The data (biscuits) are being stored in the memory location (plate, container) so that they can be accessed later for future use.
You might have noticed the data (biscuits) isn’t changing, unlike the memory locations that can be free changed.
The same thing happens in computers as well:
The CPU has memory locations where we store data.
The memory locations are known as variables whereas the data contents are known as constants.
Thus the generalized format to store data in a memory location will be:
Memory location = Data
Or, in a more particular way: Variables = Constants
This is universal for all programming languages.
Now let us talk about variables and constants in Java.
Constants are defined as the data items or values that are stored in variables for future usage and access. They are also called Literals.
Variable in Java can be defined as a memory location whose name can be varying in nature and has a value. They are also called Identifiers.
This can be represented as:
Variable = Constant / Identifier = Literal
Each variable has got a constant value associated with it. You may thus conclude that variables and constants are interdependent in nature.