Variables (Lesson 4)

The code you write as a program is a series of steps that a computer can follow to give the solution to a particular problem. During this processing done by the computer, at some stages, you find the need to store some data temporarily to process it later. You might use it to store the current situation of the program. You might direct the input from the user into a temporary memory location. This is where the concept of variables comes in.

What is a variable?
A variable is a location in the memory (memory means RAM) where you can store temporary data. I hope you already know the concept of RAM. It stores temporary data which vanishes as soon as the power supply is discontinued. Right? By declaring a variable in your program, you actually reserve a location in the memory where your program will store data. That data will vanish as soon as your program exits or just un-reserves the previously reserved memory location.

RAM and Processor?
This is somehow not related to the topic but I thing if you understand this concept here, you will know how efficient your program is. An efficient program is the one which uses less RAM and less processor. I previously told you that the program you create is actually a list of steps that a computer will follow. By following, we actually mean the processing computer has to do. It means that more the number of steps you ask the computer to perform, the more processor (processing power) it will use. Similarly, the number of variables you declare determine how much RAM your program uses.

Declaring a variable in Java:
In java code, variables are declared in the following format:
DataType variableName;
In the above statement we just declared a variable with name variableName and type DataType. You can store some value in it later. You can also initialize (give it an initial value) a variable right at the time of its creation.
DataType variableName = value;
OR if you want to declare many variables at once:
DataType variableName1 , variableName2 , variableName3
In the above example, many variables are created. They are separated by a comma. Every statement ends with a semicolon. You can also initialize multiple variables at their time of creation.
DataType variableName1 = value1 , variableName2  , variableName3 value2
Before we dive into data types, let’s take an example of declaring variables. One of the most commonly used data types is int . An int data type is used to store integer values. So, declaring an int variable will look as follows:
int myVariable = 28 ;

A program illustrating this is shown below:

Another most commonly used data type is String. A String is a sequence of characters. In java, a String is always written in double quotation marks. For example, “Java”, “Facebook”, “internet”, “abc123” are strings. If a sequence of digits is enclosed in double quotation marks, it will also be treated as a String. For example 123 is a number but “123” is a String. I’ll give you an example about this later. Firstly, let’s consider an example on String variables:
* Note that the ‘S’ in “String” is capital while the ‘i’ in “int” was small.
Let’s take another example on String variables:
In the above example, we used + to join two Strings. Here + is an operator. We will study about operators in the upcoming lessons.
Now let’s take an example on why 123 and “123” are different.
In this lesson, we studied about two data types, String and int. We will study about remaining data types in the upcoming lessons.
Rules for naming variables in Java:
It is totally up to you what name you select for your variables. But there are some rules which you must follow. If you don’t follow them there will be an error in your program. Following are the rules you must keep in mind while giving a name to a variable:
  • Variable name should not contain a space.
  • Variable name con not contain a special character except underscore _ or a dollar sign $
  • Variable name can not be a reserved word. Words like int and String are reserved words because they have a pre-defined meaning in java. You can not give another meaning or store values in them. Reserved words are also known as keywords. For a list of all Reserved words in java, see this https://en.wikipedia.org/wiki/List_of_Java_keywords
  • Variable names are case sensitive. It means that Var and var are different from each other and can be used separately. This is illustrated in the following example:
Conventions for Naming variables:
There are also some conventions for naming variables in java but these are never followed by the programmers. According to Oracle:

If the name you choose consists of only one word, spell that word in all lowercase letters. If it consists of more than one word, capitalize the first letter of each subsequent word. The names gearRatio and currentGear are prime examples of this convention. If your variable stores a constant value, such as static final int NUM_GEARS = 6, the convention changes slightly, capitalizing every letter and separating subsequent words with the underscore character. By convention, the underscore character is never used elsewhere.

Source: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html

Variables are also classified as Instance Variables, Class Variables, Local Variables and Parameters. You will learn about this classification later when you will reach the Object Oriented Programming (OOP) section. However, you can get information about them at https://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html

1 thought on “Variables (Lesson 4)”

Leave a Comment