• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
Bartenders:
  • Himai Minh
  • Carey Brown
  • Frits Walraven

operator precedence

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am new to scjp preparation group. so first hi to all..

iam not absolutely clear about operator precedence .

question:
class Sam
{
static boolean a,b,c,d;
public static void main(String arg[])
{
d = (a=true) || (b=true) && (c=true);
System.out.println(a +","+b+","+c);
}
}

output:true,false,false
doubt:&& has higher precedence than || then why || is evaluated first.

suggest me a clear guide/tutorial for operator precedence and associativity.
 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So the answer to your question goes as below

Evaluate: ((a=true) || ((b=true) && (c=true)));
Evaluate: ((true) || ((b=true) && (c=true))); // a=true is executed

As it knows true || something is always true (short circuited), the control breaks at this step without executing the further statements for b and c

So the result is a=true b=false and c=false
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java Operator Precedence and Associativity Table
What are operator precedence and the order of operand evaluation?
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic