Click here for the previous tutorial.
In the last tutorial, we had the computer tell us some message that was predetermined. It might wow your friends a little if they did not know what was going on, but it probably did not seem all that impresive. Well today is the day you officially learn how to create, set, and pass values, so you no longer need to have it display whatever message is simply typed into the parameters.
As a bit of a disclaimer, we will be working with numbers today, so while what will happen is actually more impressive than last tutorial, it may not look like it to your friends; the computer will print things like "5" and "10034". You might want to wait until a bit later if you want to amaze them again.
Now declairing (Programmer term for "Creating" the variables) variables is actually quite simple. All that you need to do is follow the simple form.
type name;
Where "type" is the type of variable, and "name" is the variable name (Remember the rules for naming classes is the same for variables). In any case, there are only a few types of variables, and the names are up to the programmer.
The types of number variables are:
int (For a whole number without a decimal)
float (For a number with a decimal)
So we can declare perfectly valid variables as follows.
int intName;
float floatName;
There are a few more types of number variables that will be brought in later, but they only differ in storage copacity. Right now, these are the only two that will be needed.
Now that we have some variables declared, there is a lot we can do with them. A computer is typically exceptional at math, and because of this it has mathematical operators embedded into the compiler. So if we were to say something like "7+4" is would know what we were talking about. Infact, "7 + 4;" is an entirely legitiment statement in the compiler, albeit useless as it does not display it or store it into memory. We can easily fix that.
To display the answer, we could add it as the argument in a Println statement.
System.out.Println(7 + 4);
Or, what might be more advantageous to us if we wished to use the sum, would be to store it in memory. Then, everytime we needed it, instead of having to calculate it again within the Println statement, we can simply call it from wherever we stored it.
To store a value in memory, we use on of our number variables. Since it is going to be a whole number, in makes sense to use the integer. In order to store it in that variable, simply use the form of assignment.
name = value;
In this case then, we would have:
intName = 7 + 4;
To use this then, we can simply pass it in as an argument. Infact, this can be used anytime a whole number could normally be used. So you can even do math with this, or even store within itself the value of it operated with another value. I know that sounds confusing, but consider this example:
intName = intName - 3;
Here we reduced its value by three, as the compiler will find the value of intName - 3, and then store that into intName. There is a faster way of doing this.
intName -= 3;
Of course, we can then assume that something like this would be valid:
intName += 1;
And infact it is. But there is an even faster way of typing this, and it is called the increment operator. Essentially, the increment operator just adds one to a number.
intName++;
It may look a bit archaic now, but in time it will look very standard. Most beginner programmers slowly start to prefer this over the "+= 1". And of course, on the same token as the increment operator there is a decrement operator, that will reduce one from the value of a number.
intName--;
On that same note of proficiency, there is also a faster way of declaring a variable if it is to have an innitial value.
float pi = 3.1415;
This statement declares a variable, and attaches a value to it, all in one line.
Finally, to finish up the tutorial on numbers, there are some operators that are useful when applying math in a program:
= (assigns the value on the right into the storage name on the left)
+ (adds two numbers)
- (Subtracts the number on the right from the number on the left)
* (multiplies two numbers)
/ (devides the number on the left by the number on the right)
% (finds the remainder of the number on the left as devided by the number on the right)
Click here to coninue to the next tutorial.
Sample code explained here:
int intName;
float floatName;
float floatName = 3.1415;
intName = 7 + 4;
intName = intName - 3;
intName -=3;
intName++;
intName--;
Always remember to keep code readible!
Variable names are subject to the creativity of the programmer, however some basic conventions are commonly followed. One of the most widely recognized naming conventions is called "camel case", and it is beginning the first word with a lowercase letter, and then every word after that with an uppercase (Example: camelCase). This is used throughout this tutorial.
Variables can be used anytime that the type that they represent could be used. For example, anytime you could use a whole number you can use an int type variable.
As mentioned, there are more types of number variables. In order of copacity (Largest to smallest)
Whole Numbers:
long
int
short
byte
Decimal Numbers:
double
float