Java Reference
In-Depth Information
double quotient;
int x = 6;
int y = 10;
quotient = x / y; // Probably wrong!
The first operation is the division, and since x and y are both integers, the result is
integer division, and we obtain 0. Integer 0 is then implicitly converted to a double
so that it can be assigned to quotient . But we had intended quotient to be assigned
0.6. The solution is to generate a temporary variable for either x or y so that the
division is performed using the rules for double . This would be done as follows:
quotient = ( double ) x / y;
Note that neither x nor y are changed. An unnamed temporary is created, and
its value is used for the division. The type conversion operator has higher pre-
cedence than division does, so x is type-converted and then the division is per-
formed (rather than the conversion coming after the division of two int s being
performed).
conditional statements
1.5
This section examines statements that affect the flow of control: conditional
statements and loops. As a consequence, new operators are introduced.
1.5.1 relational and equality operators
The basic test that we can perform on primitive types is the comparison. This
is done using the equality and inequality operators, as well as the relational
operators (less than, greater than, and so on).
In Java, the equality operators are == and != . For example,
In Java, the
equality operators
are == and != .
leftExpr==rightExpr
evaluates to true if leftExpr and rightExpr are equal; otherwise, it evaluates to
false . Similarly,
leftExpr!=rightExpr
evaluates to true if leftExpr and rightExpr are not equal and to false
otherwise.
The relational operators are < , <= , > , and >= . These have natural meanings
for the built-in types. The relational operators have higher precedence than the
equality operators. Both have lower precedence than the arithmetic operators
The relational
operators are < , <= ,
> , and >= .
 
 
Search WWH ::




Custom Search