Let's talk about variables now. Variables are very important to dynamic and interactive programs. You'll need to know how to use them in order to write any kind of worthwhile program. Java has two types of variables.
1 public class AdditionExample
2 {
3 public static void main(String [] args)
4 {
5 int firstNumber = 9;
6 int secondNumber;
7 int answer;
8
9 secondNumber = 8;
10
11 answer = firstNumber + secondNumber;
12
13 System.out.println("The numbers added together equal " + answer);
14 }
15 }
Above, I have defined a new class called AdditionExample. The starting point is the method called 'main', just as in the last lesson. However, this program has six lines of code to execute.
Line 5, int firstNumber = 9; sets aside a 32-bit spot in memory for an integer value, hereafter known to the computer as 'firstNumber'. The computer you are using probably has 256 MB of memory. This little speck of memory called firstNumber could be located anywhere within that memory bank. We don't know and don't care exactly where it is, because the computer will find it for us whever we need it. Whenever you type 'firstNumber' in the source code, the computer looks deep into its bowels for the spot of memory where firstNumber is stored. It reads the value of this small memory cell, and it will find in this case, the number 9. If you were able to look at the physical bits inside of the memory chips that are now referenced by 'firstNumber', you would see this:
0000 0000 0000 0000 0000 0000 0000 1001
Remember, an int type is 32 bits long, and the number 9 can be written with only the last 4 bits. That's the reason for the string of 28 zeroes with the 1001 tacked on at the end.
Line 6 and 7 similarly declare two spaces of memory to be set aside for the integers called 'secondNumber' and 'answer'. Notice that we haven't set these variables equal to a number, as we did with firstNumber (we'll do that later). If you don't specify what you want a variable's value to be, Java will assign it a value of 0. I could have assigned a value to these variables at this point, but I wanted to demonstrate how it isn't necessary (and sometimes it is impossible). There are typically many different ways to solve a problem with programming, and in time you'll learn the ways that most suitably fit your needs.
Line 9 demonstrates how we can set a variable's value whenever it suits our needs. Here I've set secondNumber equal to the value 8 simply as an illustration.
At line 11, we I am assigning a value to the variable 'answer'. Notice that it is not being assigned a number value directly, but rather the value of firstNumber + secondNumber.
At line 13, I am printing to the screen a text message indicating the value of 'answer'. Notice how I have combined the text message and the variable 'answer' to form a sentence. The '+' operator is interpreted here as a concatenation tool (as opposed to addition) by Java. This is a typical practice, and have many opportunities to use it.
You can pratice combining different variable types when printing sentences. I've written some examples below. Feel free to analyze them, then compile them to see if they behave the way you thought they would.
public class VariablesExample
{
public static void main(String [] args)
{
int number = 26;
char initial = 'M';
double PI = 3.14159;
System.out.println("I am " + number + " years old!");
System.out.println("My middle initial is " + initial);
System.out.println("The value of pi is " + PI);
number = 23;
System.out.println("Quince is " + number + " years old!");
}
}