Now that you know all the thinking process behind using something like variables, let us move on to taking a look at how would you declare a variable in Java.
So now we are going to take a look in code how you can declare these simple variables. So for example, if you wish to store the marks of atom inside containers, then what was the first step? Well, the first step was telling what kind of information you will be storing. So here 72 and 85 are the marks of this particular student are always going to be integers. So I'll say that the kind of information which I want to store is int. So you don't need to worry about it at this stage because int is a keyword which is used in Java to tell the program that you are going to store integers inside this. So now we are done with telling the computer what kind of information we'll be storing. The next step? Well, the next step is giving a name to these variables. For example, you want to say that this particular container or this particular variable, you want to call it like marks one. Then you say int marks one. So here it tells that you're going to store integers and this is the name of this particular variable. And how do you tell the computer that your statement is complete? Well, you put a semicolon after this. So this is a standard way to declare a new variable. This semicolon is necessary to tell the computer that this is the particular whole statement that it needs to evaluate. Next, suppose if you wish to store this other marks here, then how would you do that? Well, I say that I wish to store int, which is again the keyword for integers in Java. And I say marks two and I put a semicolon to complete this statement. So till this point, computer has assigned two separate boxes called marks one and marks two and it knows that they are always going to contain integers in it. Now, if ever in future you try to assign some other things inside these containers, it will throw an error. For example, if you try to store the name of these particular students in these two containers, then the program is going to throw an error.
Now moving on, suppose that I wish to store the name of these two students. So whenever you are storing textual information or information which is in the form of words or multiple characters, then the kind of information which you are going to store, you can tell it to the computer by saying that you are going to store a string. Here again, you need not worry about this, but for now just understand that string is a keyword which is used in Java to tell the program that you are going to store words or multiple letters in this particular variable. So now we are done with telling the computer what kind of information you're going to store. Next step is giving a name to these variables. So I say suppose Adam is my student number one. So I say string student one. And now am I missing something? Well, yes. We also have to put the semicolon here to complete this variable declaration. Similarly, now, if Lucy is student number two, then in order to declare a variable for her, I say string student two and put a semicolon. So, in this manner, I have now assigned four different containers. Two to contain the marks for Adam and two for containing the name of two students. So now we are done with learning. How do we declare variables in Java?
To declare variables in Java, first, specify the type of data you will be storing, such as integers or strings.
Integers are indicated by the keyword "int," and strings are indicated by the keyword "string."
Once you have specified the data type, give a name to the variable, such as "marks one" or "student one."
Remember to add a semicolon after the variable declaration to indicate the end of the statement.
If you try to store a different type of data in the variable, such as storing a name in a container for integers, the program will throw an error.
By following these steps, you can declare multiple variables in Java to store different types of data, such as storing marks and names of students in separate containers.
In Java, this is how you declare a variable:
You first tell the computer what kind of data you would be storing in the variable and then you assign a variable name to the variable. For example:
int distance;
Now that you are done with declaring a variable, the next logical step is to assign values to it.
This process is known as Variable Initialization.
In the following video, you will learn how to initialize variables in Java.
Initializing a variable in Java is done as follows:
<variable type> <variable name> = <value to be stored in the variable>;
For example:
int distance=0;
Till now you have learned the following:
What are variables and why do we need them?
Declaring variables in Java
Initializing variables in Java
Further on, you will learn how are the variables used in a program and some rules regarding naming variables.