Page 1 of 1

about com.enthuware.ets.scjp.v6.2.391

Posted: Sat Sep 06, 2014 8:10 am
by arius81
Hello,

Code: Select all

class Test
{
	static boolean a;
	static boolean b;
	static boolean c;
	public static void main (String[] args)
	{
		boolean bool = (a = true) || (b = true) && (c = true);
		System.out.print(a + ", " + b + ", " + c);//it will print  "true, false, false"
	}
}
I did not understand. Based on http://docs.oracle.com/javase/tutorial/ ... ators.html (operator precendence) && comes before || so should evaluate first right side "(b = true) && (c = true)" and print "true, true, true" or perhaps "false true true".


Where is my mistake?


thank you

Re: about com.enthuware.ets.scjp.v6.2.391

Posted: Sat Sep 06, 2014 9:43 am
by admin
Java language specification doesn't mention anywhere that && has more precedence than ||. In absence of any such precedence, evaluation order is left to right as explained here: http://docs.oracle.com/javase/specs/jls ... l#jls-15.7

HTH,
Paul.

Re: about com.enthuware.ets.scjp.v6.2.391

Posted: Sat Sep 06, 2014 10:10 am
by arius81
thank you Paul.

So then the table on the page ( http://docs.oracle.com/javase/tutorial/ ... ators.html )is wrong?

I read in the page : "The closer to the top of the table an operator appears, the higher its precedence." and "Operators on the same line have equal precedence". Then i see that && are closer to the top then || , and && and || not are on the same line.

Re: about com.enthuware.ets.scjp.v6.2.391

Posted: Sat Sep 06, 2014 11:01 am
by admin
It is on Oracle's official site so I am hesitant to call it wrong but I would trust JLS more than any other article or tutorial.