Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Order of Operators Precedence  RSS feed

 
AngelinaJeana Brown
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am looking at the Operator Precedence table on Oracle's website: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html.

Their table labels &, ^, | as bitwise and &&, || as logical, whereas the table on page 52-53 in my OCA book labels  &, ^, | as logical and &&, || as short-circuit. Are these terms interchangeable?

Oracle's table also lists bitwise & as having greater precedence than ^ which has greater precedence as | which has greater precedence as logical && which has greater precedence as ||.
Do I evaluate & before ^ (and so on) no matter its order in the expression?

Thanks!
 
Stephan van Hulst
Saloon Keeper
Posts: 7821
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, the terms are not interchangeable. However, &, ^ and | are overloaded for both bitwise and logical operations. When used as logic operators (meaning, with boolean operands), they don't short-circuit.
 
Henry Wong
author
Sheriff
Posts: 23284
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
AngelinaJeana Brown wrote:
Oracle's table also lists bitwise & as having greater precedence than ^ which has greater precedence as | which has greater precedence as logical && which has greater precedence as ||.
Do I evaluate & before ^ (and so on) no matter its order in the expression?


Precedence and Evaluation Order are two separate concepts. Evaluation order is defined by Section 15.7 of the Java Language Specification, and basically, goes from left to right.

Henry
 
Roel De Nijs
Sheriff
Posts: 11338
177
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This topic is an absolute must-read to learn everything you need to know about operator precedence and evaluation order.
 
AngelinaJeana Brown
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!