Wednesday, 29 August 2012

Variables: First stepping stone

          Hello again people, here we meet again! We are about to see what are variables, what are they meant for.

         First a we need something, some memory to store our data or information to process them in our program. For example, if we have a program to add two numbers, we must place the two corresponding numbers into the computer's memory to finish the calculation and get the result. Thus in order to store our data into the memory, programming languages provides something called as variables.

          Is it tough to understand? Then try this. If you are going for shopping. You need to be provided with a bag to carry your goods. Without a bag, you could not carry anything that you have purchased. So here relate the bag with the variable and goods with the data to e stored. Can you get the picture now?

          Yeah the variables are the carriers of the data which we are going to provide for calculation. Programming is all about data and its calculations. There would be no need for programming, if there are no data. Variables provide a piece of memory from our computer and allows us to store the information temporarily. After the program finish the calculation, or the execution is finished, the data stored will be gone. Variables just helps to carry data for single execution. "What if I want to run the program more than once?". Its simple. You have to provide the data for each execution of the program.

          And we have to look into another aspect. The name 'Variable' remains anything to you? In general, variable means changeable. So the same meaning is taken here too. That  means the value stored to a variable can be changed any number of time within the single execution. But once when the value is changed, the previous value could not be retained. Because, changing of value means, changing the data stored in memory. Well the old data is erased form the particular memory to store the new data. So an erased data could not be regained from the memory.

          Speaking in simple terms, you can change the items that have been previously put into you shopping bag. But when we dispose the old item, say we put it into the trash crusher. It could not be get again. You will have your newly stored item alone in your bag. That's what I was explaining above.

          Now how can we use our variable in a program? What would they look like?

          Variables can be anything of plain English letters, words or even meaning less sequence of letters and words.

   a - could be the name of a variable
   alien - could also be a variable
   aefeagfj - this could also be a variable to

          By seeing the above examples, how does a variable look like. But these are not proper naming of a variable. In Java and all other programming languages, there are some set of rules that should be followed strictly. They are know as Naming Conventions.

Here I give you the rules:

1. First and foremost thing we have to know is, Java is a case-sensitive language. Thus its variable are also case-sensitive. So be careful in naming and accessing the variables. Because the variable 'x' is not as same as 'X'. Both variables represents different memory of our computer. So a value stored in 'X' could not be accessed by the variable 'x'.

2. Name of a variable can contain digits or sequence of numbers and special characters '_' (Underscore) and '$' (Dollar Sign) alone.

3. Starting of a variable name does matters the most. Name of a variable could start with alphabets both uppercase (Capital alphabets) and lowercase (small alphabets). It may also start with the '$' or '_' symbols. But a variable name should never start with a number. Numbers could be used in the middle, at the last but not at the first of a variable name.

$name - is legal
_name - is legal but not encouraged
6name - is illegal
name6 - is legal
na6me - is legal

4. A blank space or we call it as 'White Space' is never allowed in a variable name. If you desire to name a variable with two words and to make some space between them, then you have to use the '_' character between the words.

fresh_variable - this is a legal name
fresh variable - this is an illegal name and is not permitted.

5. Even though, it is legal to name a variable with single character, It is strongly discouraged. Instead use a name that would give the actual meaning of the data to be stored into the variable. An please don't use gibberish words also. Name a variable meaningfully so that your program becomes easy to read and understand. It even helps you in coding without much frustration.

a - legal but strongly discoruged
myName - legal and much appreciated
afsndbf - strongly discoruged

6. The most important point is we cannot use the keywords of Java as a variable name. Java programming language has a set of keywords that are used for some other purpose. Those keywords could not be used to name a variable.

new - is a keyword and not allowed
New - is legal
newName - is legal and much appreciated

7. This point is just to improve the readability of your code. But it is strongly encouraged. If the name of your variable consist of only one letter, make the first letter of the variable to lowercase. But if the variable name has more than one word in it, then let the starting letter of first to be in lowercase and make all other starting letter of subsequent words to uppercase. You may also use '_' like shown above.

newname - looks gibberish and strongly discouraged
newName - looks precise and easy to read. Much appreciated

8. If you need to store a constant value (which we would be discussing later), then make every letter of the name to uppercase. This distinguishes a variable from a constant.

COMPANY_NAME - legal and much appreciated for constants

          So now have we got a basic idea about the Variables? I hope this helps you people. If there is anything that I could help you more please feel free to ask and suggest me to improve this tutorial. All your comments are welcome. Thank you friends for taking time and being patient to read. Meet you all in my next post.