It's not a secret anymore!
The moose likes Beginning Java and the fly likes Please help to Understand How Increment and Decrement works Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Please help to Understand How Increment and Decrement works " Watch "Please help to Understand How Increment and Decrement works " New topic
Author

Please help to Understand How Increment and Decrement works

rayan alex
Ranch Hand

Joined: Nov 25, 2012
Posts: 33
    
    1
Hi Please help to understand this out put of this



I counted the output as 35. But correct out put 33. Please help to understand.

This is how i counted.

++x uses incremented value. So first ++x = 11
next ++x increment x again. so second ++x = 12
next x++ uses the current value in the memory. so uses 12. 11 + 12 + 12 = 35


Thank you
Rameshwar Soni
Ranch Hand

Joined: Feb 03, 2011
Posts: 246
Since you are telling first there is ++x then there is ++x and finally x++, so the way you have written your expression is incorrect.

So instead of this
write your expression neatly i.e.


you will surely get the output as 35.
rayan alex
Ranch Hand

Joined: Nov 25, 2012
Posts: 33
    
    1
Hi,

Thank you for your reply. But correct out put is 33. I tried to say im getting 35.
I trying to figure out why i'm not getting 33 with my calculation.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16690
    
  19

rayan alex wrote:


I counted the output as 35. But correct out put 33. Please help to understand.

This is how i counted.

++x uses incremented value. So first ++x = 11
next ++x increment x again. so second ++x = 12
next x++ uses the current value in the memory. so uses 12. 11 + 12 + 12 = 35


There is no "next ++x" (ie. second pre-increment). There is a space between the two plus signs. It is not a pre-increment, it is two unary plus operators. So, the correct expressions with precedence (and associativity) applied should be...



Henry

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Please help to Understand How Increment and Decrement works
 
Similar Threads
doubts getting exponentially INCREMENTed
Be The JVM (Head First)
Please help me to figure out confusion with my program output.
post increment operator
Need Explanation of unpredicted output even if methods are synchronized