• 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
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Operators precedence

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,

Am very week in operator precedence
Can any one give clear explanation which operator should be considered first??

int a1=1;
System.out.println(4 == 2 && ++a1 < 4 );

What i think is
1. as prefix ++ operator has more precedence ++a1 shouild be evaluated
2. ++a1 < 4 should be evaluated
so on

But it first check for 4==2 and the next is not executed as it evalutaes to false

Can you please help me from this confusion
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its usually best to put ALL expressions in parans rather than trust to memory about operator precedence.

Using compiler shortcuts leads to problems!! Code it correctly from the start.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Norm. Why would you write code like this in a realy program? You should use parentheses to clarify operator precedence and so that it works the way you expect.

However, even if you add parens, there is another issue that you seem to have missed. First of all, I think the < and == operators have equal precedence. This means that the left-most is evaluated first. Note that 4==2 evaluates to false. You probably expect that < will be evaluated next. However, the && operator fits in a group called "short-circuit operators". Because the left side of the && operator evaluates to false, we already know that the complete expression will be false without evaluating the right side. Java is smart enough to know this, too, so it "short-circuits" the operation and doesn't waste any time evaluating the right side of the expression.

As a side note, the || operator works similarly. The difference is that if the left side evaluates to true then it doesn't bother evaluating the right side.

HTH

Layne
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Layne is correct. It will never evaluate the right side of an && if the left side is false. That is important in cases like

a != null && b != null && a.equals(b)

I keep a book with a tab on the precedence page on my desk as I just can't remember. But it often encourages me to forgo the parenthesis. Then when the book isisnt around and I am debugging I end up adding them anyway :roll:
[ September 20, 2005: Message edited by: Mr. C Lamont Gilbert ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic