Simple Arithmetic Operators

Now that we know how to declare and assign values to integer and floating-point data types, we can now take a look at manipulating these values using numeric operators. The standard numeric operators are shown in the table below, along with a description.

Operator

Description

*

Multiplication

/

Division

+

Addition

Subtraction

A simple example of a numeric expression is an operator, like the ones in the operator table above, with an operand on either side of the operator (for example, 5 + 2).

The value of a variable can be assigned a numeric expression using the same assignment methods that we have already seen simply using the "=" assignment operator.

int singleNumber = 4; int doubleNumber = 4 + 4; int trebleNumber = 4 + 4 + 4;

The variables doubleNumber and trebleNumber could also have been assigned using the already declared variable singleNumber with the multiplication operator.

int singleNumber = 4; int doubleNumber = singleNumber * 2;     // 4 * 2 = 8 int trebleNumber = singleNumber * 3;     // 4 * 3 = 12

You could even define the value of trebleNumber using the variables singleNumber and doubleNumber with the addition operator.

int trebleNumber = singleNumber + doubleNumber;     // 4 + 8 = 12

Subtraction is the same as addition.

int positiveNumber = 7; int negativeNumber = 0 - 7;    // or just use = -7

Or, you could make negativeNumber the negative value of the value stored in the variable positiveNumber.

int negative = -positive;     // equals -7 also

Dividing two integer values will give the answer as an integer value (that is, the actual value rounded down to the highest integer that is less than or equal to the actual value—basically cutting off anything after the decimal place).

int number = 9 / 2;           // equals 4, it does not equal 4.5

Floating-point variables use these operators in exactly the same way as integers.

double doubleNumber = 9.0 / 2.0;     // equals 4.5 float floatNumber = 9.0f / 2.0f;     // equals 4.5 

If you are assigning the value from an integer calculation to a floating-point variable, you must typecast the integer calculation to a floating-point calculation. For example, the following line of code will set the variable doubleNumber to 4 when the actual answer should be 4.5, but the calculation is an integer calculation.

double doubleNumber = 9 / 2; integer calculation equals 4

The following three lines of code all assign a value of 4.5 to the variable doubleNumber.

/* divide two double values */ double doubleNumber = 9.0 / 2.0;     /* divide two integer values casting the integer value 9 to a  double, then dividing a double by the integer value 2 giving an answer of type double */ double doubleNumber = (double)9 / 2;     /* divide two integer values casting the integer value 2 to a double, then dividing the integer 9 by this double value giving the answer  of type double */ double doubleNumber = 9 / (double)2;

Parentheses can be used to specify the order in which the values of an expression are to be calculated; this is mostly useful when you have a numeric expression that contains more than one operator (for example, 3 + 4 * 6) allowing you to choose the order in which the calculations occur. Let's now take a quick look at operator precedence.



Java 1.4 Game Programming
Java 1.4 Game Programming (Wordware Game and Graphics Library)
ISBN: 1556229631
EAN: 2147483647
Year: 2003
Pages: 237

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net