• 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

Operators

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

Following code produces result as 11.

I have confusion about it.

Can anybody give me short explanation about it.

I think output should be 12.
thanks in advance
class arithmaticTest
{
public static void main(String []args)
{
int i=10;
i=++i;
i=i++;
System.out.println(i);
}
}
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Zohaib, when you post a source code, please Use Code Tags so that the code is readable.



The question that you've asked is simple. I've commented the line where i will be 11 in the code. The next line is a bit confusing at first. The steps that will happen is this

i = i++;

first the part in the bold will be evaluated. Since this is a postfix increment operator, so the expression will result in 11. By expression I mean it will be equivalent to

i = 11;

But the assignment will not be made first. First the value will be increased. Then i will be assigned 11. So you can think of that line as



I hope it makes sense to you...
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please use code tag to post you code to make clear. Coming to your question

try(run) this and compare to your code

 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ohh Ankit beat me here . i dont see your post while i post
 
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason it behaves as observed is because of the interaction between the operator precedence and the odd "return value" of the ++ operator.
The postfix increment happens first (see your favorite copy of the operator precedence chart); but postfix, by definition, returns the previous value of the variable. So that "return value" is not "original i plus one", but instead just "original i" (i, at this point, actually contains the incremented value). This returned value is then assigned to i, which overwrites what the ++ did in the first place.
 
Abhay Agarwal
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well you have beaten me also ... seems that i need to increase my typing speed...
 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

seetharaman venkatasamy wrote:Ohh Ankit beat me here . i dont see your post while i post



Abhay Agarwal wrote:Well you have beaten me also ... seems that i need to increase my typing speed...



Yepee, I came first
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please put the question with the code brackets.
If you look at the following line

What happens? We have here a postfix increment operator. But what is the precedence between increment operator and '=' operator? The '=' assignment operator has the least precedence !!!
So what happens into more detail: First we copy the value of i (here 11) to memory, then we increment i (to 12) and as the last operation we assign the copied value to i, so i is back to 11.
So you see, if we assign the incremented value of i to another variable, let say j, then the output would be 12, like here:


At least, that's how I see the pre/postfix operators.
Hope this helps.
cheers Bob
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic