File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Mock Exam Errata and the fly likes increment operator Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Professional Certification » Mock Exam Errata
Reply Bookmark "increment operator" Watch "increment operator" New topic
Author

increment operator

eskay kumar
Ranch Hand

Joined: Jul 22, 2000
Posts: 71
Don't remember which exam i saw this one at...but i just can't seem to understand why this should happen....
It seems obvious that the foll two code should give an output: a=3,
code 1:{
int a=2;
int y= a++;
System.out.println("a="+a);
}
code 2: {
int a=2;
a=a+1;
System.out.println("a="+a);
}
However this code below outputs a=2.
{
int a=2;
a= a++;
System.out.println("a="+a);
}
Why is the post incremented value of 'a' not printed in the last code???
Help !!!

[This message has been edited by eskay kumar (edited July 23, 2000).]
Joseph Zhou
Ranch Hand

Joined: Aug 01, 2000
Posts: 129
As you see, the expression a++ is 2, and will assign to a (using =). The = have the lowest precedence than others, so, it will be the last step to be carry out.
sunilkumar ssuparasmul
Ranch Hand

Joined: Dec 13, 2000
Posts: 142
{
int a=2;
a= a++;
System.out.println("a="+a);
}

SEE it happens like this
1. a= 2 is stored in some memory location
2. a is incremented and stored in some other location
3. while assigning back it takes the original memory location i.e of step 1
4 . ie y u get output=2 if u try giving some other variable like b=a++ u would get the desired answer
thanks
sunil


------------------
"Winners don't do different things
They do things differently"

"Winners don't do different things<br /> They do things differently"
 
IntelliJ Java IDE
 
subject: increment operator
 
WebSphere development made easy
without the weight of IBM tools
http://www.myeclipseide.com