• 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

inc / dec Operations precedence

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

I'am prepairing for SCJP with ExamLab mock exams and got a Question about increment/decrement Operators.

x = 90;
y = 91;
I thought 'y' would be like : y = 45 + 45 , since it is a post-fix Operator.
So why 91?

if i change the term to:

... everything is as I expect it to be.
y = 46;


thanks for your help!

Ruediger

btw. can anyone point out, where to find this in the Java documentation?
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

is equivalent to




is equivalent to
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ruediger Froehlich wrote:

x = 90;
y = 91;
I thought 'y' would be like : y = 45 + 45 , since it is a post-fix Operator.
So why 91?

btw. can anyone point out, where to find this in the Java documentation?



You getting confused with a mix of three different things ...

1. The behavior of the postfix operator has nothing to do with its precedence. In fact, the prefix and postfix increment operator has the same precedence. The behavior of the postfix operator merely states that the expression has value (the original value), and it has a side affect (the variable gets incremented) when used.

2. Precedence is used to determine which operands and results from other expressions should an operator work on. Think of it as the compiler putting implicit parens for you. It has nothing to do with what actually gets evaluated first. In this case, the postfix increment has higher priority than the add operator.

3. The order of evaluation is the actual order that the expression gets evaluated. And generally, it is done left to right.


So...

y = y++ + y;

Apply precedence...

y = ((y++) + y);

BTW, the assignment has the lowest precedence.

Evaluate left to right...

y = ((45)) + y);

The increment operator is encountered, it has a value of 45, and it has a side affect of incrementing y (So, y is now 46).

y = (45 + 46);

The result of the post increment is 45. Then the next operand is encountered, which is y, which has a value of 46.... and at this point, I think you can figure the rest for yourself.

Henry
 
reply
    Bookmark Topic Watch Topic
  • New Topic