Special Effects and Game Development
in Java(TM)  Variables and numbers
by Anibal Wainstein
1.2 Variables and numbers
Most of the Java programs use some kind of numbers. We will
now look at the most commonly used numbers
1.2.1 Variables
A variable is a storage space or memory where you place information.
You can handle the variables on your program by calling them
by names. You never need to know where the information is
placed when you store it in the variable, but you always use
the variables' names to fetch it and store information. It
is like that when you ask someone to store something. Next
time you need it you just say the name of the thing and the
person will get it from its hiding place. When you are adding
a variable into a program you must always declare them.
The declaration consists of the type and the name you will
call it by.
variabletype name;
There are different types of variables like integers, strings,
image variables and more. In the declaration above you probably
noticed that I wrote a semicolon ";". You must
always end a statement with a semicolon or else the
compiler will complain. One stores and fetches information
on a variable in the following way:
variablename1 = value; (this will store the value in "variablename1")
variablename2 = variablename1; (fetches the value from "variablename1"
and stores it in "variablename2")
1.2.2 Integers
An integer is a number that is in the range of ±2^{32}
( 4294967295 though to 4294967295). Assume we want to set
the variable "a" to the number 1. The syntax is
the following:
int a = 1;
1.2.3 Arithmetic operators
The number types in Java would be worthless if you could
not use the arithmetic rules on them. Assume that we have
the variables "a" and "b" where we have
store the numbers 4 and 2:
int a = 4;
int b = 2;
int c;
We have also declared the variable "c" here, where
we will put or store the results of the calculations. Note the
syntax we are using in order to store the information in "a"
and "b". We have first declared and then stored the
number in the same statement. This is a shortcut but it is practically
the same as: int a;
a = 4;
int b;
b = 2;
int c;
Now we want to do an operation and store the result in the variable
"c". The arithmetic operations work in the following
way (note that "*" is the multiplication operator):
//The number 6 will be stored in "c"
c = a + b; //The number 2 will be stored in "c"
c = a  b; //The number 8 will be stored in "c"
c = a * b;
//The number 2 will be stored in "c"
c = a / b;
The two slashes "//" indicates that the text that
follows is a comment. Let us assume that we now have a more
complex mathematical formula that we want to calculate:
The syntax for the formula above can be written with parenthesis
like this:
c = ((a + b) / (a  b)) * a + 1;
Assume that we now want to operate "b" on "a"
and store the result in "a". You can write:
//add "b" to "a"
a = a + b;
//subtract "b" from "a"
a = a  b;
//multiply "a" with "b"
a = a * b;
//divide "a" with "b"
a = a / b;
You will get the same result if you write: //add "b" to "a"
a += b;
//subtract "b" from "a"
a = b;
//multiply "a" with "b"
a *= b;
//divide "a" with "b"
a /= b;
Let us say that you want to increase or decrease "a"
with 1. Then there are many ways to do this:
increase "a" with 1 decrease "a" with 1
a = a + 1; a = a  1;
a += 1; a = 1;
a++; a;
The later method is called postincrementation (++) and postdecrementation
() and we will use this very often. There is also something
called preincrementation and predecrementation but we will
not look at that here since you will manage well by just using
postincrementation and postdecrementation.
Next Page >>
