Monday, March 25, 2013

Java;Notes - 6.) Variables in Java

In Java when you declare a variable you must also declare its type. Here are a few examples:
  • double salary;
  • int vacationDays;
  • long earthPopulation;
  • boolean done;
NOTE: The use of a semicolon when you see on it means it is the official end of that line of code. Meaning a single line can be any number of lines of the page.

Variable Rules:
When dealing with variables in Java keep the following things in mind:
  1. Variables must begin with a letter
  2. Variables must be a sequence of letters and digits (yes you can use single letter sequences such as "int x = 2.34;" and it will work)
  3. Variables can not contain spaces or symbols such as: @#$% and so on.
  4. In proper naming convention if you have a variable that explains something and uses a mix of names such as a variable for positive and negative numbers its a good idea when naming them to make the first letter of the variable lowercase and them capitalize the first letter of each part after that Like so:
    1. Incorrect but will still work: int posnum; 
    2. Correct: int posNum;
    3. Correct more than one part: int numberOfCakesInShop; (I think you get the picture now)
  5. If you are going to be using more than one of the same data types add them all to the same line. Say for instance again you have a positive number and a negative number. Instead of listing each number type on its own line. Use comma's to put them all on the same line like so:
    1. int posNum, negNum, answer; //Doing it like this will save you space on your code. instead of one line for each just do it like this!
  6. There are occasions where you will have a variable and a type that can only be described one way simply say for instance you have Type: Box and variable: box you can go "Box box;" and call it a day but that might get messy after a while. Instead add a number (To the back) or a letter to the front of the variable like so: "Box aBox;" or even "Box box1;" and these will suit and give a good idea if you have more than one of what ever variable you are using.
Initializing a Variable:
In java you can not just make a variable and start printing it like so:
  1. int vacationDays;
  2. System.out.println(vacationDays);  <--- This will lead to an error telling you to initialize it. 
Initializing Variables is actually rather simple as all you are doing is giving it a meaning just do the following:
  1. int vacationDays;
  2. vacationDays = 12;
Now the variable vacationDays has a meaning and thus something to print out. 
What if I told you there was a better more simple way to do that? Well there is... you can do it all on one line like so:
  1. int vacationDays = 12;
Using the last two methods you will now be able to use the previous print statement to make the screen print out "12" both ways work the same.
Note: you can put variable declarations anywhere in your code. Usually it is wise to keep them near the top so you have an easy reference place.

Constant Variables:
This is not as complex as it sounds it simply means your telling your program of a variable that (For this program) will never change. To do this we make use of the final declaration. Say for instance we are making a program to measure the circumference of a circle. What is one common factor in this calculation.... You guessed it... Pi (Funny... a Pie can also be a circle!) so say we want to make pi a constant... you simply do the following:
  1. final float pi = 3.14159;
and now pi will not change... less you edit that code line for a different number. 

No comments:

Post a Comment