Hi all, According to Simon Robert's TCJ2 Certification Study Guide, the bitwise operator (& ^ |) has greater precedence order than short-circuit operator (&& | |). Would that mean if we had if (expression1 && expression2 & expression3) { }; that means the expression2 and expression3 gets evaluated first? or, is it two different thing, order of precedence and order of evaluation? Please consider the following code:
I've compiled and run it, and it gives the output of: increment x1 false decrement a0 b-1 false It appears that the evaluation order is always from left to right, and the && operator will always short-circuit. Please LMK what you think... Thanks in advance.. - eric
Manfred Leonhardt
Ranch Hand
Joined: Jan 09, 2001
Posts: 1492
posted
0
Hi Eric, Yes your synopsis is correct. The evaluation order is not the same as order precedence. Accoring to the JLS spec 15.7:
The Java programming language guarantees that the operands of operators appear to be evaluated in a specific evaluation order, namely, from left to right.
Futhermore, 15.7.1 goes on to say the following:
The left-hand operand of a binary operator appears to be fully evaluated before any part of the right-hand operand is evaluated. For example, if the left-hand operand contains an assignment to a variable and the right-hand operand contains a reference to that same variable, then the value produced by the reference will reflect the fact that the assignment occurred first. Thus: class Test { public static void main(String[] args) { int i = 2; int j = (i=3) * i; System.out.println(j); } } prints: 9 It is not permitted for it to print 6 instead of 9.
Regards, Manfred.
Eric Pramono
Ranch Hand
Joined: Jul 09, 2001
Posts: 74
posted
0
Hi Azaman, I think your only problem was you implement the show() method incorrectly. I've tried and declare show() method in Sub class as public instead, and it's working correctly for both scenarios. So, I guess non-static inner class can still extends an explicit class and implements an interface. - eric
Eric Pramono
Ranch Hand
Joined: Jul 09, 2001
Posts: 74
posted
0
Oops sorry.. Please ignore my previous post.. wrong thread.. Thanks Manfred for your explanation, but could anyone give me an example on how to illustrate the & higher precedence over && ? Any help is appreciated. - eric
Mini Pilla
Ranch Hand
Joined: Jul 15, 2001
Posts: 112
posted
0
Hope this explains what u wanted.... public class Q8 { public static void main(String[] args) { Q8 a = new Q8(); boolean b=true; boolean c=false; boolean d=true; boolean e=b&&c&d; boolean f=b&c&&d; System.out.println("e"+e ); System.out.println("f"+f); } }