Variables and Simple Math
Variables in Java are memory locations that can hold values. There are 8 primitive types in Java.
- byte - An 8 bit integer
- short - A 16 bit integer
- int - A 32 bit integer
- long - A 64 bit integer
- float - A 32 bit floating point number
- double - A 64 bit floating point number
- char - A single 16 bit unicode character
- boolean - A boolean value that can be either true or false
The differences between the integers is how large a number they can store. A byte, containing only 8 bits, can only hold a number between -128 and +127, whilst a short can hold between -32768 and +32767, an int -2147483648 and +2147483647, and a long can hold between a whopping -9223372036854775808 and +9223372036854775807. Likewise, a float can hold a floating point number up to 9sf (significant figures) multiplied by a power of 10 between -45 and +38; while a double, taking twice as much memory, can hold a floating point number of up to 18sf multiplied by a power of 10 between -324 and +308.
An integer is represented as a simple number such as "200" or "57888". Longs are represented by adding an "l" or "L" to the end of a number, like "57777777779L" or "-46666335l". Doubles are simple decimal numbers like "3.4", "23.0" or "4.2899e10". Floats are represent by adding a "F" or a "f" onto the end of a decimal, like "3.0f" or "24.78f". Chars are represented by a unicode character inside single quote marks, like 'B', '6' or '$'. A boolean value is represented by the word true or the word false.
Bytes and short cannot be represented normally, instead they must be "cast", which we will cover soon.
Intialising & Using Variables
Variables can be intialised by a line stating the variable type and variable name.
They can then be used to store values using the equals ("=") operator. In this case, the equals sign acts like an arrow pointing left; it takes the value on the right of the sign and puts it into the variable on the left.
number = 700034;
bignumber = -5468998779877L;
decimal = 48.0f;
accuratedecimal = 2.33e56;
letter = 's';
possible = true;
You can also store values when you are initialising the variables.
int number = 700034;
char letter = 's';
After variables have values stored in them, they can be used anywhere where a normal number, letter or boolean respectively would normally go.
int number = 52;
will have the same effect as
Basic calculations can be performed on integer and float variables with the following operations:
- + Addition
- - Subtraction
- * Multiplication
- / Division
- % Modulus
The only operation which might require some explanation is the modulus operator. The modulus operator can only be used on integers and gives the remainder of integer division. So as 17 divided by 6 gives 2R5, 17 / 6 will return 2, while 17 % 6 will return 5. When using the modulus operator on floats the right hand variable is subtracted from the left hand variable until the left hand variable is less then the right. i.e. 5.5 % 2.1 will give a result of 1.3.
These operations can be used wherever a number would normally appear, and can be used on variables as well as normal numbers. All the following are valid.
int number = 5 + 7;
System.out.println(3 * 5);
int othernumber = number / 7;
System.out.println(number + 56 % 8);
In the case where there are more than one operation used as in the last line, the calculation follows the normal order of algebra, performing multiplication, division and modulus before addition and subtraction. You can use brackets to force the order that they are performed in, the inner most brackets will be evaluated first.
System.out.println((6 * 3) - (25 / 5));
The Java compiler is quite picky when it comes to types. Ints can only be multiplied by other ints, floats with floats, doubles with double and so on. This can be quite a problem when we want to mix types. We can convert one type to another by doing something called casting. You can cast a double to a float, an int to a float, or pretty much any of the numbers into each other. You can cast by putting the required type in brackets before the variable, or by putting the variable in brackets after the type.
int number = (int) 26.7;
Note that when casting from a floating point number to an integer, the number is not rounded, instead the decimal portion is lost. So the int version of 26.7 is 26, not 27.
Java will cast automatically when it expects one type and gets another only when it will not result in any possible data loss. So floats can be stored in doubles and will be automatically converted to doubles when they are multiplied by doubles, but you can't store a double in a float without casting.
double number = (double)10.0f; // These two lines
double number = 10.0f; // are both legit
float number = (float)10.0; // This is fine
float number = 10.0; // The compiler will issue an error at this
Basically a number will automatically promote itself up this list, but has to be cast to travel down.
The Java language provides several simple shorthand notations for common mathmatical patterns.
number = number + 6;
is equivalent to
number += 6;
and similarly with -=, *=, /= & %=.
is equivalent to
number += 1;
and similarly with number--, known as incrementing and decrementing. However ++number and --number also do the same thing. The difference here is that post-increment (number++) returns the original value of number before incrementing while pre-increment (++number) add 1 on to the value and then returns it. This is only useful when acutally using the value it returns.
int number = 5;
will print 5, while
int number = 5;
will print 6. The value of number will be equal to 6 after the statement in both cases.