In the same way we can mix and match Strings variables and String literals, we can mix and match number variables and number literals, for example, each of the following additions are valid:
int a = 4 + 3; //a is 7
int b = a + 7; //b is 14
int c = a + b; //c is 21
As well as the plus operator, we can also use the symbols for subtraction -
, multiplication *
, and division /
. We should remember that working with integers will result in an integer as the result - see the example below:
int result = 9 / 4; // result is 2, not 2.25
Sometimes it is useful to find the remainder of the division of an integer. In this case we use the modulo operator %
, for example
int sweetsInPacket = 23;
int noOfChildren = 3;
int sweetsPerChild = sweetsInPacket / noOfChildren; // result is 7
int sweetsLeftForDad = sweetsInPacket % noOfChildren; //result is 2
If a calculation causes a integer or long value to go higher than the maximum permissible value, then the number will 'overflow', this means if we are have a integer with a value of 2147483647 (its maximum value) and add 1 to it, its value would become –2147483648 (its lowest value). If we were to add 4, it would be –2147483645 - for example:
int a = 2147483647;
a = a + 1; //a is now -2147483648
Because calculations in floating point numbers do not use (or emulate) the base 10 numbering system humans use, calculations involving decimal numbers can result in unwanted side-effects for calculations involving currency. Consider the following example:
double x = 4.4;
double y = 6.6;
double z = y/x;
One would consider z should equal 1.5, however if we output the value we would see it as 1.4999999999999998
One solution for calculations using currency is to work with the integer (or long) data type, but work with values in pence instead of pounds (or the smallest unit in other currencies).
Sometimes we may create values that should not, under any circumstances, be changed later in the code, examples of such values might include Pi, or the number of minutes in an hour. To indicate that a value may not be changed, we simply add the keyword final before the variable type as follows:
final double pi = 3.14159265359
int daysInAYear = 365;
You've already seen that changing the type of data used in calculations can have an effect on the output - unless we specified that 'milliseconds in a seconds' variable was 'long' the calculation took place in the limited range of an integer, and the calculation went wrong when its maximum value was exceeded.
Java (unlike some languages) allows us to mix numeric data types when doing calculations. Arguably this makes in simpler to perform calculations, but as we've already seen can allow errors to be introduced.
It is not enough, therefore to specify the data type we want for the result of a calculation, we must consider the types of values used in the calculation.
int resultA = 9 / 4;
int resultB = 9.0 / 4.0;
double resultC = 9 / 4;
double resultD = 9.0 / 4.0;
double resultE = 9.0 / 4;
double resultF = 9 / 4.0;