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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
arius81
Posts: 3
Joined: Sat Sep 06, 2014 7:52 am
Contact:

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

Post 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

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

arius81
Posts: 3
Joined: Sat Sep 06, 2014 7:52 am
Contact:

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

Post 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.

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 83 guests