aspose file tools*
The moose likes Beginning Java and the fly likes i=i++; could anyone explain this....please? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "i=i++; could anyone explain this....please?" Watch "i=i++; could anyone explain this....please?" New topic
Author

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

Akhil kumarS
Greenhorn

Joined: Oct 16, 2007
Posts: 13
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?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

http://faq.javaranch.com/java/PostIncrementOperatorAndAssignment


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Akhil kumarS
Greenhorn

Joined: Oct 16, 2007
Posts: 13
Thank you Henry....this was what i searching...thanks a lot.....
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
Ultimately, what you likely wanted was



(or some cleaner version thereof)
[ November 24, 2007: Message edited by: Bill Shirley ]

Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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.)


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Akhil kumarS
Greenhorn

Joined: Oct 16, 2007
Posts: 13
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
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
dibyarajan mallick
Greenhorn

Joined: Oct 24, 2008
Posts: 2
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

Joined: Oct 27, 2005
Posts: 19720
    
  20

Please Don't Wake The Zombies. In case you hadn't noticed, the last post of this thread was almost 11 months old.
dibyarajan mallick
Greenhorn

Joined: Oct 24, 2008
Posts: 2
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.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
Thank you very much for explaining, EFH.

And welcome to JavaRanch, ( ), dibyarajan mallick. Please note Rob's point about old threads.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: i=i++; could anyone explain this....please?