The first stepping stone to computer programming is understanding what are variables. Be it in Java or any other programming language you may choose, variables play a very important role in your program. Variables are storage units in your program and facilitate a major chunk of computations you will be doing.
Through the video below, you will understand what variables are and why do we need them.
So till now you have learned how to write simple print statements in Java. But merely printing text or other information is not sufficient. You can do a lot more with computer programs. For example, if you have a list of certain students who enrolled in a certain course and have secured some marks in a particular subject, then you can store that information also and then later on reuse it. So what is the advantage of storing information? Well, computers are basically used for storing and doing a lot of calculations. So do you recall what happened when we wrote a simple print statement? For example, if you wrote a simple print statement to print the name of a student, that information was printed on the console and it got immediately lost. That is, you could no longer use the name of the student anywhere later in the program. But just imagine a scenario where you are writing a big program in which you have to maintain a list of a large number of students with a large number of marks. So if you keep writing print statements for them, there is no point because all that information is not stored anywhere. So if you need to store any kind of information, you obviously need some containers to store that particular information. For example, if there is a student with the name Adam, and if there is another student with the name Lucy, and if you simply print them, then this information gets immediately lost. However, if you store the names of these students in certain containers, then later on you can use their names somewhere later in the program as well. Moreover, you can do a lot more stuff. For example, you can store their marks in a certain subject and later on use those stored containers to make other calculations.
So this brings us to one of the most fundamental and basic building blocks in Java programming languages, which we call as the variables. So basically, variables are nothing but simply containers in which you can store these pieces of information. You can imagine them as boxes or containers which will contain the names of these students. They can also contain suppose, marks of these students and a lot more. So we are going to take a look at variables in a little more detail now. So you can imagine Java variables as being a safe house of any information which you want to retrieve. So we say that variables are nothing but containers which can contain some information. For example, I might have these two containers which will store the marks of a certain student. So suppose student Adam has marks 72 in one subject and 85 in another subject. Then I can store this 72 and this 85 into separate containers. So these two separate containers will be called as two separate variables. And why am I doing this? Well, I'm doing this because perhaps later, somewhere in the program, I would need to do some calculations on these variables. Perhaps I would need to calculate what are the total marks he scored. So in that case, I can simply access the marks of Adam which are contained in these two containers and add them.
In Java programming, it's not enough to just print information, you also need to store it for later use.
Storing information is important because computers are used for storing and doing calculations.
If you only print information, it gets immediately lost and cannot be used later in the program.
To store information, you need containers called variables, which can store information such as student names and marks.
Variables are like boxes or containers that can hold information and can be accessed later for calculations.
Java variables are containers that can store any type of information and can be used to retrieve information later in the program.
You now know what variables are. Variables are simply containers in the computer memory to store the information you want.
In the following video, you will understand how variables are named and why is this important.
Now you understand that these containers or variables need to be named. But can all types of information be stored in the same type of container? How do we tell the computer about the size of information to be stored in a container?
You will get the answer to these questions through the following video.
Till now, you must be clear on the definition of variables:
Variables are spaces in a computer’s memory that have names associated with them, where you can store data
You need to tell the following two things to the computer about a variable:
Name of the variable
The kind of information you would want to store in this variable
The process of doing so is known as variable declaration.
In the next segment, you would learn writing commands in Java to declare variables and also to store values in them.