Java Reference
In-Depth Information
- The subtraction ( - )
- The multiplication ( * )
- The division ( / )
- The modulo (remainder, % )
These operations depend on the type of operands, and yield the usual bugs.
For example, consider the variable q defined as:
int q=2/3;
This initializes the integer variable q to 0 since the division / is the Euclidean
division. 5
Programmers have to take care with variable initializations, especially when
implicit casting occurs. To illustrate these points, consider the following code
snippet:
double qq=2/3;
double qqq=2/3.0;
Although variable qq is declared of type double the division operands 2 and 3
have been identified as integers so that the compiler will compute the Euclidean
division and get the integer 0 , which will then be implicitly cast into a double
(see Figure 1.1): 0.0 . However, when declaring and initializing double variable
qqq , since the second operand is of type double , the first operand will be cast
into a double , and the double division will be performed yielding the expected
result: 0 . 6666666666666666.
The operators unambiguously satisfy priority rules so that parentheses may be
omitted when forming expressions for ease of reading. For example, consider the
expression 7+9*6 . This expression admits two kinds of parentheses: (7+9)*6
and 7+(9*6) . But since the multiplication has higher priority over the addition,
it is understood that the expression 7+9*6 is meant to be 7+(9*6), and
evaluates to 61.
As we will see in the next chapter, an important class of expressions are boolean
expressions , which are used in program control structures. Boolean expressions
admit only two outcomes: true or false . The most common logical operators
are && for AND and for OR. Thus for boolean variables a and b , the boolean
expression a&&b; is evaluated to true if and only if both a and b are true .The
following program presents the use of boolean expressions and boolean variable
assignments:
5 In Euclidean division, x/y computes the Euclidean ratio of x by y ,and x%y
computes the remainder.
 
 
Search WWH ::




Custom Search