Java Reference
In-Depth Information
// An error. 2.0 and 3.2 are of the type double. The result of 2.0 + 3.2 is 5.2,
// which is also of the type double. double to float assignment is not permitted.
f1 = 2.0 + 3.2;
// Ok. 2.0F and 3.2F are of the type float. The result of 2.0F + 3.2F,
// which is 5.2F, is of the type float.
f1 = 2.0F + 3.2F;
// Ok. j is promoted to float and f1 + j is of the data type float.
// float to double assignment is permitted.
d1 = f1 + j;
Subtraction Operator (-)
The subtraction operator (-) is used in the form
operand1 - operand2
The subtraction operator is used to compute the difference of two numbers, for example 5 - 3 results in 2 .
All rules that I discussed about the numeric data conversion of the operands and the determination of the data type
of the expression involving the addition operator are also applicable for an expression involving subtraction operator.
The following are some examples of using the subtraction operator:
byte b1 = 5;
int i = 100;
float f1 = 2.5F;
double d1 = 15.45;
// Ok. 200 - 173 will be replaced by 27.
// b1 = 27 is ok, because 27 is in the range -128 and 127
b1 = 200 - 173;
// An error. i - 27 is of the type int. int to byte assignment is not allowed
b1 = i - 27;
b1 = (byte)(i -27); // OK
Multiplication Operator (*)
The multiplication operator (*) is used in the form
operand1 * operand2
The multiplication operator is used to compute the product of two numbers, for example, 7 * 3 results in 21 .
All rules that I discussed about the numeric data conversion of the operands and the determination of the data type
of the expression involving the addition operator are also applicable for an expression involving the multiplication
operator. The following are some examples of using the multiplication operator:
byte b2 = 5;
int i2 = 10;
float f2 = 2.5F;
double d2 = 15.45;
 
Search WWH ::




Custom Search