File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes i=i++; Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "i=i++;" Watch "i=i++;" New topic
Author

i=i++;

weiliu lili
Ranch Hand

Joined: Apr 11, 2002
Posts: 46
public class Inc{
public static void main(String argv[]){
Inc inc = new Inc();
int i =0;
inc.fermin(i);
i = i++; /*if i change the code in here as%i=i;i++;%the output is 1 ,
i thought the two expressions are the same as %i=i++;%,but actually they are different,why? */
System.out.println(i);
}
void fermin(int i){
i++;
}
}
Jamal Hasanov
Ranch Hand

Joined: Jan 08, 2002
Posts: 411
Hi, Weiliu
Because ++ in "i++" is postfix operator.
Try compie/run this and analyse for yourself:
class test{
public static void main(String []arg) {
int i=0;
System.out.println(i++);
i=0;
System.out.println(++i);
}
}
Additionally read RHE.
Best Regards,
Jamal Hasanov
www.j-think.com
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

As Jamal stated... i++ is the postfix increment operator. It doesn't return a result so that's why the expression i=i++; doesn't work --> i++ on its own increments the variable.
Also... check out the handy dandy UBB [CODE] tags that allows you to preserve whitespace in your code and improve readibility.
[ May 30, 2002: Message edited by: Jessica Sant ]

- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
Jamal Hasanov
Ranch Hand

Joined: Jan 08, 2002
Posts: 411
Also visit this threads for additional info:
http://www.coderanch.com/t/191679/java-programmer-SCJP/certification/Maha-trick-hairy
http://www.coderanch.com/t/191291/java-programmer-SCJP/certification/Its-hairy-again
Jamal Hasanov
www.j-think.com
Younes Essouabni
Ranch Hand

Joined: Jan 13, 2002
Posts: 479
Originally posted by weiliu lili:
public class Inc{
public static void main(String argv[]){
Inc inc = new Inc();
int i =0;
inc.fermin(i);
i = i++; /*if i change the code in here as%i=i;i++;%the output is 1 ,
i thought the two expressions are the same as %i=i++;%,but actually they are different,why? */
System.out.println(i);
}
void fermin(int i){
i++;
}
}

As everybody noticed it's a post operator, it means that the value of i will be changed later.
But it's value is changed
So
i=i++;
System.out.println(i)// Will print 2
and
i=1;
System.out.println(i++)// Will print 1
System.println(i)// Will print 2
The incrementation is taking effect after the evaluation.
I hope it helps!


Younes
By constantly trying one ends up succeeding. Thus: the more one fails the more one has a chance to succeed.
Swati Gupta
Ranch Hand

Joined: May 28, 2002
Posts: 106
When I was in this thread I found the link to a very good thread where Maha Anna had explained about increment and decrement.
I am planning to appear for Exam in few days and wondering that do we have any list of these types of good threads then me going through each thread.
Thanks.
Vikrama Sanjeeva
Ranch Hand

Joined: Sep 02, 2001
Posts: 756
Try This Link.It discusses the same code in good detail.
Bye,
Viki.


Count the flowers of your garden, NOT the leafs which falls away!
Prepare IBM Exam 340 by joining http://groups.yahoo.com/group/IBM340Exam/
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: i=i++;