Post Page Advertisement [Top]

Given: integer x = 40, y = 35, z = 20, w = 10 Comment about the output of the following two statements: print x * y / z - w print x * y / (z - w)

Q5. Given:integer x = 40, y = 35, z = 20, w = 10
Comment about the output of the following two statements:
print x * y / z - w
print x * y / (z - w)

a. Differ by 80
b. Same
c. Differ by 50
d. Differ by 160

Answer a.

1.60

2.140   (Difference is 80)

Explanation:

in first Print statement 

Operator Precedence in Java

Java has well-defined rules for specifying the order in which the operators in an expression are evaluated when the expression has several operators. For example, multiplication and division have a higher precedence than addition and subtraction. Precedence rules can be overridden by explicit parentheses.

Precedence order.

 When two operators share an operand the operator with the higher precedence goes first. For example, 1 + 2 * 3 is treated as 1 + (2 * 3), whereas 1 * 2 + 3 is treated as (1 * 2) + 3 since multiplication has a higher precedence than addition.

Associativity.

 When an expression has two operators with the same precedence, the expression is evaluated according to its associativity. For example x = y = z = 17 is treated as x = (y = (z = 17)), leaving all three variables with the value 17, since the = operator has right-to-left associativity (and an assignment statement evaluates to the value on the right hand side). On the other hand, 72 / 2 / 3 is treated as (72 / 2) / 3 since the / operator has left-to-right associativity.

Precedence and associativity of Java operators.

The table below shows all Java operators from highest to lowest precedence, along with their associativity. Most programmers do not memorize them all, and even those that do still use parentheses for clarity.

OpratorDescriptionLevelAssociativity
[]
.
()
++
--
access array element
access object member
invoke a method
post-increment
post-decrement
1left to right
++
--
+
-
!
~
pre-increment
pre-decrement
unary plus
unary minus
logical NOT
bitwise NOT
2right to left
()
new
cast
object creation
3right to left
*
/
%
multiplicative4left to right
+ -
+
additive
string concatenation
5left to right
<< >>
>>>
shift6left to right
<  <=
>  >=
instanceof
relational
type comparison
7left to right
==
!=
equality8left to right
&bitwise AND9left to right
^bitwise XOR10left to right
|bitwise OR11left to right
&&conditional AND12left to right
||conditional OR13left to right
?:conditional14right to left
  =   +=   -=
 *=   /=   %=
 &=   ^=   |=
<<=  >>= >>>=
assignment15right to left
There is no explicit operator precedence table in the Java Language Specification and different tables on the web and in textbooks disagree in some minor ways.

Order of evaluation of subexpressions.

Associativity and precedence determine in which order Java applies operators to subexpressions but they do not determine in which order the subexpressions are evaluated. In Java, subexpressions are evaluated from left to right (when there is a choice). So, for example in the expression A() + B() * C(D(), E()), the subexpressions are evaluated in the order A()B()D()E(), and C(). Although, C() appears to the left of both D() and E(), we need the results of both D() and E() to evaluate C(). It is considered poor style to write code that relies upon this behavior (and different programming languages may use different rules).
Short circuiting. When using the conditional and and or operators (&& and ||), Java does not evaluate the second operand unless it is necessary to resolve the result. This allows statements like if (s != null && s.length() < 10) to work reliably. Programmers rarely use the non short-circuiting versions (&and |) with boolean expressions.

Precedence order gone awry.

 Sometimes the precedence order defined in a language do not conform with mathematical norms. For example, in Microsoft Excel, -a^b is interpreted as (-a)^b instead of -(a^b). So -1^2 is equal to 1 instead of -1, which is the values most mathematicians would expect. Microsoft acknowledges this quirk as a "design choice". One wonders whether the programmer was relying on the C precedence order in which unary operators have higher precedence than binary operators. This rule agrees with mathematical conventions for all C operators, but fails with the addition of the exponentiation operator. Once the order was established in Microsoft Excel 2.0, it could not easily be changed without breaking backward compatibility.

Bottom Ad [Post Page]

| Designed by Colorlib