This is a chapter that attempts to tie up some loose ends regarding variables. This is the section of the tutorial to examine when something goes wrong with your variables and you want to trouble-shoot the program.
Variable names always obey the following rules:
Here are some legal variable names:
GoodVariable Accounts2000 this_is_a_legal_variable_name$
Here are some illegal variable names:
Bad Variable 1999Accounts $why_is_this_one_wrong?
You will also find that there is a list of names that you can't use for variables, because they are reserved words in Java, i.e. they are used for special purposes. You have already met some of them, e.g. extends, double and extends. Here is a complete list of the reserved words - don't use them for naming variables:
abstract |
boolean |
break |
byte |
case |
cast |
catch |
char |
class |
const |
continue |
default |
do |
double |
else |
extends |
false |
final |
finally |
float |
for |
future |
generic |
goto |
if |
implements |
import |
inner |
instanceof |
int |
interface |
long |
native |
new |
null |
operator |
outer |
package |
private |
protected |
public |
rest |
return |
short |
static |
super |
switch |
synchronized |
this |
throw |
throws |
transient |
true |
try |
var |
void |
volatile |
while |
||
You have met some of these, and will meet more later on in the guide.
A variable of type int can take any value from -2 147 483 648 and +2 147 483 647 inclusive. A variable of type float can take any value from -3.4 x 1038 (which means -340 000 000 000 000 000 000 000 000 000 000 000 000 in plain language) to +3.4 x 1038 (which means +340 000 000 000 000 000 000 000 000 000 000 000 000). Variables of type float are held in the computer's memory to about 7 digits of accuracy. On the other hand, double variables are stored to about 15 digits of accuracy.
You have to remember that the numbers cannot be stored to an infinite precision. However many digits the computer used to store the number, there are some decimals that it simply can't store perfectly. This means that you should occasionally be prepared for inaccuracies to appear in the last few decimal places of your numbers. There's not a great deal you can do about it, I'm afraid, but most of the time it doesn't make that much difference.
That's about it, really!
The following instruction may seem a little strange:
myVariable = myVariable + 1;
How can a variable be the same as itself plus one? Well, you must remember that this is assignment, not seeing if two things are equal. What this statement means is "take the current value of the variable, add one to it and that is what the new value of the variable is." Effectively, it increases the value of the variable by 1.
This operation is called incrementing the variable. Because it is such a common thing to do, Java provides a simpler way to do it:
myVariable++;
No need for an equals sign, or the figure 1. There isn't even a space between the variable name and the two plus signs. This instruction means "Increase the value of the variable myVariable by 1". There is a similar instruction to decrease the value of the variable by 1 (called decrementing):
myVariable--;
Java also provides five operators which change a variable's value by adding, subtracting, multiplying, dividing and taking the remainder on division, by some value other than 1. The following instruction, for example, adds 2 to the variable's value:
temp+=2;
Similarly, the following instruction multiplies the variable's value by 10:
age*=10;
Use -= to subtract a number from a variable, and /= to divide a variable by a given number. You can even turn a variable into the remainder when division has been carried out:
x = 20; x%=3; // x becomes the remainder when x is divided by 3 // x now has the value 2
We have already seen that statements such as the following are illegal in Java:
int x = 2.6;
You just can't assign a decimal value to an integer variable like that (although it is perfectly legal the other way round - assigning an integer value to a floating point variable). However, there are occasions when you do need to panel-beat a variable, a number or a mathematical expression from one type to another. This is known as a cast and it is done by specifying the required type in brackets at the start of the expression, and enclosing the expression itself in brackets:
double x = 56.01; float y = (float)(2 * x);
In this case, the variable x is a double variable, so the expression 2 * x will produce a value which is also a double. We want to put this value into a float variable, y, so we enclose the expression in brackets and preface it with (float).
Similarly, this expression converts a floating point value (read from a variable), chops off the decimal part at the end and puts the resulting integer part into an int variable. The original variable temp1 is not altered in any way - it stays as a float variable:
float temp1 = 3.0f * y; int temp2 = (int)(temp1);
In this case, of course, part of the variable's value is lost when it is copied into the integer variable - the decimal part.