In Java when you declare a variable you must also declare its type. Here are a few examples:
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.
When dealing with variables in Java keep the following things in mind:
Variables must begin with a letter
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)
Variables can not contain spaces or symbols such as: @#$% and so on.
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:
Incorrect but will still work: int posnum;
Correct: int posNum;
Correct more than one part: int numberOfCakesInShop; (I think you get the picture now)
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:
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!
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:
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:
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:
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.
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:
final float pi = 3.14159;
and now pi will not change... less you edit that code line for a different number.