File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes short circuit operators Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide 1Z0-808 this week in the OCAJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "short circuit operators" Watch "short circuit operators" New topic

short circuit operators

Soni Prasad
Ranch Hand

Joined: Mar 09, 2005
Posts: 97
Which operator's precedence is more between && and ||.

SCJP 1.4, SCBCD 1.3
Niki Nono
Ranch Hand

Joined: Mar 20, 2005
Posts: 256
neithers precedence is more i think.
I believe the expression is read from left to right and parantheses are taken into consideration.

Life called,so here I am.<br />Cheers<br />Niki.:-)
Mathangi Shankar
Ranch Hand

Joined: Nov 01, 2004
Posts: 56
&& has higher precedence than || operator.

In the bitwise operators folloing is the sequence of precedence of operators in descending order




--------------------------------<br />SCJP1.4
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
Details can be refered to JLS Chapter 15.7 Evaluation Order .


SCJP 1.2, OCP 9i DBA, SCWCD 1.3, SCJP 1.4 (SAI), SCJD 1.4, SCWCD 1.4 (Beta), ICED (IBM 287, IBM 484, IBM 486), SCMAD 1.0 (Beta), SCBCD 1.3, ICSD (IBM 288), ICDBA (IBM 700, IBM 701), SCDJWS, ICSD (IBM 348), OCP 10g DBA (Beta), SCJP 5.0 (Beta), SCJA 1.0 (Beta), MCP(70-270), SCBCD 5.0 (Beta), SCJP 6.0, SCEA for JEE5 (in progress)
Soni Prasad
Ranch Hand

Joined: Mar 09, 2005
Posts: 97
ok Mathangi,
I was also thinking the same but after solving this question I got a bit confused

class EBH202 {
static boolean a, b, c;
public static void main (String[] args) {
boolean x = (a = true) || (b = true) && (c = true);
System.out.print(a + "," + b + "," + c);

ans is true,false,false
udhaya kanagaraj
Ranch Hand

Joined: Mar 31, 2005
Posts: 62
hai Soni Prasad,
Before answering ur question I will give the order of precedence.
|,&,||,&&.This will be the order of precedence for the operators.
Answering for the code given by u,
the shortcircuit_OR operator won't check for the second value if the first value itself is,if the first value is false then only it will
check for the second in ur code since a is assinged to true,it won't check b and c.while trying to print b and c it will print the default
value faulse.If ur code is like this
then the output will be
Mathangi Shankar
Ranch Hand

Joined: Nov 01, 2004
Posts: 56
Hi Prasad,

In evaluation of boolean expressions involving conditional AND and OR, the left-hand operand is evaluated before the right one, and the evaluation is short-circuited.

I hope now it is clear.


udhaya kanagaraj
Ranch Hand

Joined: Mar 31, 2005
Posts: 62
hai mathangi,
sorry.I think u r wrong .b'cos shortcircut OR will be given more precedence than shortcircut AND.

Joined: Mar 28, 2005
Posts: 23

In Khalid Mughal book it is given that && has more precedence than || operator. However when iam executing ur code iam getting true,false,false
even if iam changing the code as

boolean x = (a = true) || ((b = true) && (c = true)); iam getting the same result. In this case right hand operator should be evaluated first,then b becomes true and then || is evaluated. How come iam getting the same result.can any one please explain.

amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
i think the code executes from Left to right as written in K&B

the answer you is getting is just because || is first written on lefthand side ....and the rest is not evaluating...

if && is written first then 2nd oprand will be evaluated....

check examples in K&B book

u all will be clear


Thanks and Regards, Amit Taneja
M Rama
Ranch Hand

Joined: Mar 04, 2005
Posts: 91
Ok, so what have we concluded.

|| and && would be left to right , whichever comes first.

while bitwise & has a higher priority over |.

How about some more examples:

results would be:

1. i1=2 j1= 2
2. i1=1 j1= 1
3. i1=3 j1= 3
Vlado Zajac
Ranch Hand

Joined: Aug 03, 2004
Posts: 245
Assume a,b and c are boolean expressions.
&& has higher precedence than || so (a || b && c) becomes ( a || (b && c)).

This has nothing to do with the order in which the expression is evaluated, which is left to right. So a is evaluated first and if it evaluates to false, b && c is evaluated.

If && had lower precednce the expression would become ((a || b) && c).
I agree. Here's the link:
subject: short circuit operators