• 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

Explain on the unary ++

 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class InitTest
{
public static void main(String[] args)
{
int a = 1;
int b = 1;
a = a++; //(1)
b = b++ + b; //(2)
System.out.println(a+ " "+b);
}
}

The above program prints 1 3 as output. which is right, but At (1) the value will assign 1 to a and incriment. my under standing was a will update with value 2. why it is having only 1 value?
In case os (2), b is incrimented and placed at right hand operand?
Could you please explain???
Thanks,
Seenu
 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Hope this will help you
 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class InitTest
{
public static void main(String[] args)
{
int a = 1;
int b = 1;
a = a++; // Works as a=1; a=1+1
b = b++ + b; //Works as b= 1 + 2; The current value 1 is used first time, then incremented before using is second time.
System.out.println(a+ " "+b);
}
}
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
somewhere in one of the java reference links, this was explained very nice.

y = x++

this operation is performed as :

take the value of 'x' and save it
increment the value of x by 1
assign the original value of x to y

based on that
x = x++

would evaluate as, value of x after the above expression would
be the original value of x.

Hope this helps.
 
reply
    Bookmark Topic Watch Topic
  • New Topic