All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.
This question is based on 2 concepts:
1. i = = ++j is not same as i = = j++;
In the case of i == ++j, j is first incremented and then compared with i. While in the case of i == j++;, j is first compared with i and then incremented.
2. The | operator, when applied for boolean operands, ensures that both the sides are evaluated. This is opposed to || which does not evaluate the RHS if the result can be known by just evaluating the LHS.
Now, let us see the values of i and j at each step:
int i = 0;
int j = 1;
if( (i++ == 0) [b]&[/b] (j++ == 2) ) //compare i with 0 and increment i => returns true and i becomes 1. Evaluate next condition:
//compare j with 2 and increment j => return false and j becomes 2.
//true & false returns false so i= 12 is not executed.
{
i = 12;
}
System.out.println(i+" "+j)); //print 1 and 2
This is not the same. The answer should be: 12 1 in stead of 1 2
yes, the explanation is misleading. The answer ist still correct but for a differetn reason: the && operator does not ensure that both the sides are evaluated. But in this case it needs to evaluate the second side also as the first side comes out as true.