# short circuit operators

Ranch Hand
Posts: 97
Which operator's precedence is more between && and ||.

Niki Nono
Ranch Hand
Posts: 256
neithers precedence is more i think.
I believe the expression is read from left to right and parantheses are taken into consideration.

Mathangi Shankar
Ranch Hand
Posts: 56
&& has higher precedence than || operator.

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

&,^,|

Thanks,

Mathangi

Nicholas Cheung
Ranch Hand
Posts: 4982
Details can be refered to JLS Chapter 15.7 Evaluation Order .

Nick

Ranch Hand
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
Posts: 62
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 true.ie,if the first value is false then only it will
check for the second value.so 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
x=((a=true)||(b=true))&&(c=true)
then the output will be
a=true
b=false
c=true
o.k.
regards,
udhaya

Mathangi Shankar
Ranch Hand
Posts: 56

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.

Thanks,

Mathangi.

udhaya kanagaraj
Ranch Hand
Posts: 62
hai mathangi,
sorry.I think u r wrong .b'cos shortcircut OR will be given more precedence than shortcircut AND.
regards
udhaya

challa
Greenhorn
Posts: 23
hi,

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.

regards
purnima.

amit taneja
Ranch Hand
Posts: 813
hi
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

:-)

M Rama
Ranch Hand
Posts: 91
Ok, so what have we concluded.

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

while bitwise & has a higher priority over |.

results would be:

true
1. i1=2 j1= 2
true
2. i1=1 j1= 1
---------
true
3. i1=3 j1= 3