Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Is the table with operator precedence correct?

 
Greenhorn
Posts: 1
Mac OS X IntelliJ IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

In the book “OCA - Oracle Certified Associate Java SE Programmer I - Study Guide” by Jeanne Boyarsky and Scott Selikoffs, I found in chapter 2 page 53 in the table with operator precedence, that logical operators &, ^and | have the same level of precedence. The table of operator precedence on Oracle Java site: Operators use different levels of precedence.

I tested the precedence with a small code snippet:
a = 5;
b = 8;
c = 2;
d = 7;
System.out.format("a & b ^ c | d = %d & %d ^ %d | %d = %d.%n", a, b, c, d, a & b ^ c | d);
System.out.format("a ^ b | c & d = %d ^ %d | %d & %d = %d.%n", a, b, c, d, a ^ b | c & d);

The output is
7
15

If the operators would have the same level of precedence, the last output should have been 7, right?

The operators &&  and || should also have different levels of precedence.

Best Regards
Henrik
 
Bartender
Posts: 1248
86
Hibernate jQuery Eclipse IDE Angular Framework Spring MySQL Database AngularJS Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch !!  

Henrik Teinelund wrote:If the operators would have the same level of precedence, the last output should have been 7, right?


Yes correct. Since all of them kept on the same line appears they are having same level of precedence as like we have additive operators ( + and - ) in the table on the same line where both are having same level of precedence.

But remember on page number 64 book states that for the exam you don't need to know numeric bitwise comparisons. As of logical operator dealing with boolean expression, I don't think order of &, ^ and | matters.

The operators &&  and || should also have different levels of precedence.


Agreed !  && precedes || ....

 
Saloon Keeper
Posts: 22289
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Practical tip:

While it's important to know the Java precedence rules for certification, in real life depending solely on operator precedence can be hazardous. This is especially true if, like many people, you're called to work with multiple languages, since not all languages assign the same precedences to the same operators. So when in doubt, parenthesize.
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic