What are variables in programming?
“Variable” comes from a Latin word, variābilis, with “vari(us)“‘ meaning “various” and “-ābilis“‘ meaning “-able”, meaning “capable of changing” – Wikipedia
Now we have a good idea of the basic java types. But in the previous tutorial, we didn’t study how we can use these types in java programming. For that, we have to study the idea of a variable as well.
Remember the elementary school math where you wrote x = 5?
There, x is a variable. This x can be any value. For example, you can rewrite it as x = -10, x=0, … and so on.
Think of a box. You would like to name it as “x”.
Now you can add something in to the box. You can remove it anytime. But the box “x” don’t change. Only its contents are changing. A variable is something like that.
In programming, instead of a box, we name x to a certain memory location in computer where we are able to put some valid data in to that memory.
In java, the creation of a variable undergoes 2 stages.
- Declaration of the variable
- Assigning a value to the variable
In the first step, if you want to create a variable named “num” capable of holding an integer, you are required to mention the type of “num” first.
Now this is the point where we come to an agreement about the standard size of the box that we are gonna use to store the value.
num = 3;
num = 24098732;
num = 3+402-32;
num = 23*20^x;
where x is another integer with a certain value. (* indicates multiplication and 20^x indicates the index of x to the base 20. that is 20x – we’ll cover this part soon!)
whatever a valid integer value.
Please note that the valid range of int in java is from -2147483648 to 2147483647
Both of these two steps at once is also possible.
Eg: int num = 3432;
Ok guys. that’s all for the basic understanding of variables.
In the next tutorial we are about to learn some rules and tips about variables which are really REALLY important.