78 Introduction to Java Applications Chapter 2 Operator(s) (Top ten web hosting)
78 Introduction to Java Applications Chapter 2 Operator(s) Operation(s) Order of evaluation (precedence) () Parentheses Evaluated first. If the parentheses are nested, the expression in the innermost pair is evaluated first. If there are several pairs of parentheses on the same level (i.e., not nested), they are evaluated left to right. *, /and % Multiplication Evaluated second. If there are several of this type of Division operator, they are evaluated from left to right. Modulus +or Addition Evaluated last. If there are several of this type of oper- Subtraction ator, they are evaluated from left to right. Fig. 2.17Precedence of arithmetic operators. g. 2.17 Now, let us consider several expressions in light of the rules of operator precedence. Each example lists an algebraic expression and its Java equivalent. The following is an example of an arithmetic mean (average) of five terms: abc++ de ++ Algebra: m = ————————————– 5 Java: m = ( a + b + c + d + e ) / 5; The parentheses are required, because division has higher precedence than that of addition. The entire quantity (a+b+c+d+e)is to be divided by 5. If the parentheses are erroneously omitted, we obtain a+b+c+d+e/5, which evaluates as abc ++ –e ++ d 5 The following is an example of the equation of a straight line: Algebra: y = mx + b Java: y = m * x + b; No parentheses are required. The multiplication operator is applied first, because multiplication has a higher precedence than that of addition. The assignment occurs last, because it has a lower precedence than that of multiplication and division. The following example contains modulus (%), multiplication, division, addition and subtraction operations: Algebra: z = + pr %qw /xy Java: z = p * r % q + w / x -y; 1 2 4 3 56 Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01
Note: In case you are looking for affordable and reliable webhost to host and run your business application check Vision php5 hosting services