• 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

i=i++; could anyone explain this....please?

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Test{

public static void main(String args[]){
int i=0;
i=i++;
i=i++;
System.out.println(i);

}
}


This prints 0...why? actually i++ will increments the value of i?
 
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
http://faq.javaranch.com/java/PostIncrementOperatorAndAssignment
 
Akhil kumarS
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Henry....this was what i searching...thanks a lot.....
 
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ultimately, what you likely wanted was



(or some cleaner version thereof)
[ November 24, 2007: Message edited by: Bill Shirley ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bill Shirley:
...or some cleaner version thereof...


Like i += 2 ?

Note that the behavior of the original code is caused by the assignment combined with the postfix increment. So you could use a prefix increment instead:

i = ++i;
i = ++i;

Or even the postfix increment without the assignment:

i++;
i++;

(Note: You can not use i++++ or even (i++)++, because the postfix operator must be applied to a variable, and it results in a value.)
 
Akhil kumarS
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually i know this will print output zero.....i want to know hw it is produced?Thats why i asked the Question?I know that the assignment i=i++;
has no effct but would like to know the working flow ...thats why ,Any way i should thank all who replied to my question,i got the answer from the link iven by henry,which was i looking for,so a special thanks to him
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the background, something like the following occurs:

This happens because with i++, first the value of i is evaluated and only then i is increased.

If you change it to ++i you get the following:

This is because with ++i, first i is increased and afterwards its value is evaluated.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not the post-increment operator that does the trick. It's not even the lower-precedence-of-assignment-operator. It's the way java executes statements.

I know you didn't get it. When JVM sees a high priority statement is interrupting, it memorizes the current execution, performs the high priority task and then executes the memorized execution.

Example :-


If you run the above code, you will understand that post-increment operator is not the one playing the trick.

Also, if you print the result returned by the method m() below, you will know that Assignment operator has nothing to do with it as well. Because, a return interrupted by finally also does show this weird behavior.



Well the reason is, the execution goes like this -
1. return statement is memorized first. so all JVM memorizes is 'return 0;'
2. now finally is executed. The variable c's value does change actually.
3. prints updated value of c i.e. 12.
3. memorized task is executed. so, 0 is returned.


Similarly while executing c=(++a/b) in first example, what JVM does is -
1. store c=2/2.
2. increment a. so a = 3, b = 2.
3. execute c=2/2. so c = 1. a = 3, b = 2.
4. print values of a, b & c.

hope you got it.
 
Rob Spoor
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please Don't Wake The Zombies. In case you hadn't noticed, the last post of this thread was almost 11 months old.
 
aziz malik
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. But, I had a different view on the topic and I think people who don't know the answer yet (like I didn't a few minutes ago) and still searching for the answer may get benefited from this.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, the biggest problem is that, unfortunately, your "different view" is wrong. People are rarely helped by learning a wrong way to think about a problem. There is no such thing as a "high priority task (or instruction)" -- this is something somebody just made up, and it's wrong. If somebody explained things to you this way, you need to go tell them to stop making up nonsense.

As far as the order of execution and exactly what gets stored for later use, it's easy enough to prove that it works the way Rob says by using the "javap" disassembler that comes with the JDK.

This code

int a=2, b=2;
int c = a++/b++;

disassembles to the following. I'll add comments on each line in italics to explain:



As you can see, the division and assignment happen after both a and b have been incremented, as Rob says, and in direct contradiction to your stated order. All code on the right hand side of the assignment statement has executed before the assigment is made, including side effects.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much for explaining, EFH.

And welcome to JavaRanch, ( ), dibyarajan mallick. Please note Rob's point about old threads.
 
You would be much easier to understand if you took that bucket off of your head. And that goes for the tiny ad too!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic