To an experienced coder these two topics probably don’t seem to have much in common, the reason I am going over them in this tutorial is that they are both important concepts that I didn’t want to write 2 seperate tutorials for.
Ok. Now your computer stores data as different data types for example it sees the number value 3 in a completely different way to the sentence “Hello World”. So what is a variable. A variable is a place in your computer where you can store information. Pretty easy so far huh? Well now comes the important part: Each data type has a variable type associated with it. The most relevant are listed below.
|Description and Usage|
|char||char is short for character. This type of variable can store a single character of text. For example ‘A’ , ‘Z’ but not “Hello” (note I used ‘ to mark a char this is important and if you don’t you will get error when you compile)|
|int||int is short for integer or whole number. Ths type of variable can be used to store whole number values. e.g 2 , 3 , 543 , -13 but not 34.3 or “hello”|
|String||this is a variable type that holds strings of text. For example “Hello World” or “Goodbye” You could store a number in this kind of value but you wouldn’t be able to do any maths with it as the computer sees it as text. (note I used ” to mark a String this is important and if you don’t you will get errors when you compile )|
|float||this is a variable type for storing floating point numbers or fractional numbers. For example 16.2 or -11.654 are all valid float variables.|
|boolean||this is an interesting one. A boolean value can only hold true or false (in lowercase) More on this in the tutorial on loops.|
You should learn the data in the table above so you know it off by heart. There are some other data types such as double which aren’t really relevant at this stage. If you want to know more about them have a look at Sun’s Java Site.
Now, on to using variables. To store information in a variable you have to do 2 things.
- Give the variable a name
- Tell your computer what data type will be stored in the variable
This process is called declaring variables. A general form of declaring a variable is
<data type> <name> = <value>;
Pay special attention to the semi colon at the end of the line. Each seperate statement in Java must have a semicolon (;) at the end of it. This is VERY IMPORTANT!!!.
Below are examples of declaring variables:
char myChar = 'A'; (Creates a char named myChar with value A)
int age = 18; (Creates an int named age with value 18)
String name = "John"; (Creates String named name with value John)
float g = 9.81; (Creates a float named g with value 9.81)
boolean isMale = true; (Creates a boolean named isMale with value true)
Once a variable has been declared you can do what you want with it. For example print it. System.out.println(name); would print the value stored in name to the screen. However System.out.println(“name”) would just print the text “name”. If the variable is of numerical type you can perform mathematical operations on it. For example the code: age = age + 12; would add 12 to the value stored in age. (age += 12; is a shorthand way of achieving this)
Everybody has their own programming style. This is the way in which they layout their code, define variables etc. However as very often people other than the programmer will want to go through the source code at a later date (to for example change a feature or update the program). If you don’t tell them how your mind was thinking when you were programming the software then its going to make their task 100 times more difficult!!
This is why comments come in useful. Comments are pieces of text which explain what the code is doing. They are ignored by the computer when you compile the program so there are no unexpected side effects. Now in Java there are 2 ways to define a comment
//this is a comment
This is a single line comment and can hold such information such as what a variable represents.
/*This is called a block comment
* and can be used to write extended
This is a block comment. These are useful when writing at the top of your program what the code does or writing above function headers exactly what the function does and what parameters it takes.
It is important not to overcomment your code. Take a look at the following example:
// add five to age
age = age + 5;
This comment is redundant as you would expect anybody editing your code to know how to add to a variable. Only include comments if you think it will improve the clarity of your source code.