A computer is a data processing machine. It takes data and processes the data based on what the user wants.
Programming is a way of telling the computer what to process.
It will be difficult to be a computer programmer if you don’t have a good understanding of how the computer takes in data in programming.
And you can imagine how easy it will be when you know how and when to use variables in your program.
This is an introduction to the topic of variables in computer programming using Java as an example.
You will have the basic knowledge of what is meant by a variable, few examples and how to use them at the end of this post.
Let’s get started.
Variable in computer programming is the part of the code that stores data.
As I have written above, programming is used to instruct the computer what to process. And an important part of this instruction is the data.
Where we keep this data in the program is what is known as a variable.
Take your wardrobe as an example, you use it to keep your wears.
There are many sections in the wardrobe and where you keep each clothing is different.
You won’t keep your shirt where you are to keep your trousers. Likewise, you won’t put the shoes in place of your knickers.
The same thing is for the computer programming variable.
It is like a wardrobe that keeps data and just like we have different types of clothes, there are different types of data as well.
Variable Types In Java
Since there are different types of data, we need different types of variables to keep these data.
In Java, there two primary types of variables.
The Primitive Variable and The Classes.
What you need to know now is the Primitive types because it is the basic and it is less complex than the Classes types.
Primitive Types Of Variable
This variable type is divided into three, Boolean, Numbers, and Char.
This is the variable that stores True or False data.
In Java, a Boolean variable is declared like this:
boolean man = false;
man in the above programming statement is the name of the Boolean variable and the variable is set to ‘false’.
The name is used to reference the variable.
So you don’t need to re-declare the variable if you want to use it in any other part of the program.
This is the second type of Primitive variable and it is further divided into two since there are different types of numbers.
We have Real Numbers and Integer Numbers
Real numbers are the numbers with a decimal point. Like 3.0, 1000.1, 1.5 etc.
There are two ways of declaring real number in Java.
Using the ‘double’ or ‘float’.
double area = 2 * 2.147 * radius;
you can use both double and float in the above statement, the only difference is that float can store bigger values than double.
In most cases, double is sufficiently used.
As you might have guessed, these are the numbers without decimal numbers.
For example, 2, 10, 5500, and so on.
You can declare integer numbers in four different ways in Java programming but the ‘int’ is the integer variable type that is normally used out of the four.
int ball = 3;
3 is the data stored in the int variable named ball.
Other integer variables that can be used in place of int are byte, short, and long.
In conclusion to this section, int and double are the commonly used variable types when storing numbers in Java programming.
A bonus point:
Use int when you want to deal with numbers with decimal point and calculations that have division.
This is just the short form of writing ‘Character’.
A Character can be anything, number, sign, alphabets etc, basically anything that the computer keyboard can type in.
This is the end of the post but wait! Please, I need one help from you, kindly share this post as much as you can if you learn one thing from it.
You should also share it if you know you have a programming friend that might like it or loves anything related to computer programming.
Thank you 🙂