Java - basic rules for arithmetic, variables and conversion
Archive - Originally posted on "The Horse's Mouth" - 2010-11-10 06:30:53 - Graham EllisDeclare - Initialise - use - the sequence for Java variables
• All Java variables must be declared before they are initialised
• All Java variables must be initialised to a value before their current value is used
So:
1. Declare (i.e. define the type of variable and the name
2. Set an initial value
3. use the value that you have stored
int rovers;
rovers = 3;
total = rovers + city;
Full example [here]
You can declare and set initial values all in a single statement:
int rovers = 1, city = 2;
int total = rovers + city;
Full example [here]
Variable names in Java must start with a letter, followed by as many or as few letters, digits, underscores as you like. Capital letters are different to lower case letters - i.e. you may consider the alphabet to have 52 letters in it as far as Java is concerned. You must not use keywords from the language as java variable names - if and class and public are invalid variable names for example. There are conventions too - whether or not to start a name with a capital, whether to make it all capital, or "camel case" where words in the middle are capitalised ... and some of those conventions are assumed to have been followed by certain tools, meaning that you MUST apply the convenion (as if it were a rule) if you want to use the tool.
Basic rules with numbers in Java
Multiplications and Divisions happen before additions and subtractions, and within each of those groups they work left to right - so
3 * 4 + 5 * 6
= .... 12 + 30
= .... 42
3 + 4 * 5 + 6
= .... 3 + 20 + 6
= .... 29
You can add brackets to change the precedence order - brackets are done first - so
(3 + 4) * (5 + 6)
= .... 7 * 11
= .... 77
If you do an integer (whole number) division, the result will be a whole number too:
3 * 4 / 5 + 6
= .... 12 / 5 + 6
= .... 2 + 6
= .... 8
If you save an int number into a float or a double, that's OK and the conversion is done silently for you (it's known as coercion) since it's always possible and there's no loss of precision. The same thing applies when converting from a float to a double, from a byte to a short, or to an int or to a long, and so on.
If you save something into a variable type that will lead to a loss of precision, or if you are trying to store something in a variable type that not all values from the first type could go into, you must give Java permsission by casting or converting. That applies if you're trying to put a double into a float, a float into an int, or a long into a short, for example.
There's an example [here] showing a compile time failure when I tried to push a double into a float. And there's an example [here] where I got the wrong results because the precendence order was wrong and I did an integer division, throwing away the remainder when I should not have done so.
I have provided two sample average programs that work - [here] where I have used specifically a 2.0F (i.e. float) constant rather than just 2.0 (defaults to double) and [here], where I let the calculation coerce to double precision then cast it to float. A third approach would simply have been to store the result into a double variable.