• 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

increment post and pre, when used in println

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


Can anyone explain lines 1, 2, 3 and 4 and differences.
Thanks.
 
Sheriff
Posts: 9707
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
Lets go through the execution step by step


int x=2;
int y=1;

System.out.println(y+++x);
This will be interpreted as ((y++)+x) This is because the compiler will parse the expression from left to right. postfix ++ operator is left associative so first two + out of +++ will be associated with y. the one + left will be a binary operator between y++ and x.
But the output will be 3 as it will be interpreted as 1+2. This is because the value of y will be 1 in this expression. After using the value of y in the expression y will be incremented.

Now x = 2 and y = 2
System.out.println(y+ ++x);
This one is simple. It will work as 2+3. Here the value of x will be first incremented and then used in the expression.

Now x = 3 and y = 2
System.out.println(++x);
This one is again easy. The value of x will be incremented from 3 to 4 and then displayed.

Now x = 4 and y = 2
System.out.println(x++);
In this one the value of x will be used in the expression and then incremented. So 4 will be displayed and then the value of x will become 5.

I think you have confusion between pre and postfix increment operators. Consult some book for this or google it...
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi SekBar prefix and postfix is strange =)
Ankit excellent explanation...

Remember: The book (K&B)pg:289: "The operator is placed either before (prefix) or after (postfix) a variable to change its value."

I advise the Debug Code for greater understanding.
Look: http://www.janeg.ca/scjp/oper/prefix.html
 
Sekhar Choudary
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you guys.
Sekhar.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic