• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Operator Precedence - Increment and Decrement Operators (Java OCA 8 Programmer I Study Guide, Sybex)

 
Rui Ribeiro
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Folowing the order precedence table 2.1 existing on page 52/53, the post-unary operator precedes the pre-unary operator.
In the example for increment and decrement operator, on page 59


why are the expression evaluated first and not the ?


Thank you
 
Henry Wong
author
Marshal
Pie
Posts: 22124
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Evaluation Order is defined by section 15.7 of the JLS ... and not by precedence or associativity.

https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.7

Henry
 
Roel De Nijs
Sheriff
Posts: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rui Ribeiro,

First of all, a warm welcome to CodeRanch!

Rui Ribeiro wrote:why are the ++x expression evaluated first and not the x--?

That's really a very common question (confusion) on this forum! You should definitely read this topic and this one as both topics provide excellent explanations (with illustrative code snippets) about the same code snippet. And if you want to learn more about the order of evaluation of (post-)increment (& decrement) operators, this topic is also definitely worth reading. And if you want to learn more about evaluating boolean expressions with short-circuit operators this topic and this one are must-read topics.

Hope it helps!
Kind regards,
Roel
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic