This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes If and parentheses 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 this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "If and parentheses" Watch "If and parentheses" New topic
Author

If and parentheses

Sam Samson
Ranch Hand

Joined: Oct 08, 2011
Posts: 63

Hi

Could someone tell me how parentheses are virtually set in this example? Is there a simple rule like 'if there are no parentheses, the first to checks build a pair'?
I mean, will z > 1 && y >6 be together or y > 6 || z == 2?




greez
Sam
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 63351
    
  70

It all has to do with the precedence of the operators.

Java operator precedence


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Sam Samson
Ranch Hand

Joined: Oct 08, 2011
Posts: 63

Thank you, that was quick

So in my example


z > 1 && y >6 is a pair

and in:

y > 6 | z == 2 would be a pair, right?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 63351
    
  70

The easiest way to figure it all out is to use the operator precedence and start adding parentheses around the highest to lowest terms.

For example, if you started with 2 + 3 * 4, you'd end up with (2 + (3 * 4)). Give it a try.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11882
    
  18

And this is why you should always use parens. really, there is zero cost to adding them, and having them saves the next person reading it (which could be yourself) the trouble of having to remember the correct operator precedence.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 20166
    
  46

Bear Bibeault wrote:The easiest way to figure it all out is to use the operator precedence and start adding parentheses around the highest to lowest terms.


To add to that, if you encounter two operators with the same precedence, then you use the associativity. Most go from left to right, but some goes right to left.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 63351
    
  70

P.S. I agree with fred -- for any but the most trivial of expressions, I use parens to make the meaning of the expression crystal clear.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8663
    
  24

Sam Samson wrote:if(z > 1 && y > 6 | z == 2)

It may also be worth pointing out that a construct like y > 6 | z == 2 is extremely rare except when dealing with bit manipulation; it's almost always better to use '||'.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 4101
    
  19

All the point are very valid, but personally I also find it handy to remember a small list so you don't get too surprised when you're reading other people's code. From highest to lowest priority:

1. Dereference (array[i] and obj.foo)
2. Unary (includes casts)
3. Arithmetic (includes shifts)
4. Comparison (includes instanceof)
5. Bitwise
6. Logical
7. Assignment

For instance, this is one I frequently bump my head into: (Something)something.getFoo(); when they mean: ((Something)something).getFoo(), because dereferences (. or []) have a higher precedence than casts.


The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: If and parentheses
 
It's not a secret anymore!