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.
A program illustrating this is shown below:
- 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:
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.
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