Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

what happens when a++ is given...where does the value stores

Status
Not open for further replies.

manohar143

Programmer
May 6, 2002
5
US
public class test
{


public static void main(String args[])
{

int a=2;
System.out.println("a"+a);
a=a++;
System.out.println("a"+a);
a=++a;
System.out.println("a"+a);



}

}

Result of this program is
a 2
a 2
a 3

How come the value of a is not changed after the statement a=a++;
I know that first it will assign the value and then increment. But in next statement i am trying to print the value of a still 2 . As far as i know after assigning the value of a is 2 then post increment process is done. So where the value of a is stored that time.
 
if you defined
int a=2;
int b=a++;

you'd expect to have b=2, not three.

When doing a=a++; you are doing the same type of operation, creating a new 'a' that is affected the old value of 'a', which is then incremented before being lost.

To avoid all confusion, you should just use the shorthand 'a++'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top