Variables and DataTypes
Imagine you are going to a trip to a place. Think if you need water in between the travel, what will you do?
If you have stored water in the bottle and carried that from home for travelling, it would be easy for you to use it whenever needed. Right.
Simply variable is like a bottle and value is like the water. To store water you need a thing like bottle, like this to store a value you need a variable.
If you want more water you will carry a big bottle else small. Right ?
In programming , the size like water bottle size is said as memory. The compiler allocates memory or sizes or spaces based on what we ask.
It allocates memory on a specific available address in primary storage or RAM.
With above example we can say that,
value - water
variable - bottle to store water
datatype - size of the bottle
For eg. If you want to create a application which needs to store a form data. eg Name, Address, etc. and use it later in the program, we must use variables to store values like name and datatypes to say to the compiler how much memory or size you need.
How will you say ?.
With the declaration of variables we can say this. Below is the way of declaration
To assign a value to the variables we can use like this below. This is said as initialization.
variable_name = value;
Both initialization and declaration can also be done in one line like below.
datatype variable_name = value;
Variable allocates memory only in RAM to store value. This will be cleared or deleted when program ends. To store data permanently we have many other ways.
You will learn naming conventions of variables, types of variables and datatypes in Java ellaborately in upcoming topics.