• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

doubt related to output involving precedence of | and &&

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! every one,

I have a doubt related to precedence order in the following code snippet.


public class PrecedenceTrial {
public static void main(String...args)
{



if(fun1() && fun2() | fun3())
{
System.out.println("inside if block");
}


}


public static boolean fun1()
{
System.out.println("inside fun 1");
return true;
}




public static boolean fun2()
{
System.out.println("inside fun 2");
return true;
}


public static boolean fun3()
{
System.out.println("inside fun 3");
return false;
}



}

when i am executing the program i get

inside fun 1
inside fun 2
inside fun 3
inside if block
but i am not able justify this As | (Bitwise OR operator) is having higher precedence than && (logical AND operator) So, fun2() should be associated with | operator and fun2() and fun3() should be evaluated first as they are operands for | operator and && should be evaluated afterwards.
but i am not getting the result as per to my view.

Please clarify it and correct me wherever i am wrong.

Regards

Jolly
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jolly,

Please read this previous post.

Mihai Fonoage
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jolly,

what you are saying is correct. '|' has higher precedence then '&&' and Associativity is left to right. That means the expression left to | will evaluate first.

In this example "if(fun1() && fun2() | fun3())" expression "fun1() && fun2()" is left to that | so it will evaluates first then fun3(). To evaluate this "fun1() && fun2()" first it will execute fun1(),if its true then it will execute fun2()

i hope this will clear your doubt
[ July 16, 2008: Message edited by: winay Kumar ]
 
Jolly Tiwari
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mihai, for sharing such a nice link .

Now i understood that evaluation must happen before applying precedence rules and in exceptional cases like operators && || and ?: ,right side
operands can be skipped for evaluation due to short circuit behaviour.


Regards

Jolly
 
He's my best friend. Not yours. Mine. You can have this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic