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 PostFix operator 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 "PostFix operator" Watch "PostFix operator" New topic
Author

PostFix operator

Prabhat Kumar
Greenhorn

Joined: Jan 25, 2007
Posts: 27
Dear all,
I am confused due to the output of the sollowing program.

public class PostFix
{
public static void main(String args[])
{
int i=0;
i=i++;
i=i++;

System.out.println(i);
}
}

The output is coming 0 .
Can any one explain why?

Thanks in advance,
PrabhatKumar

outpu is
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Perhaps have a look at
http://www.coderanch.com/t/263804/java-programmer-SCJP/certification/Java-Increment-operator


By the way, is "Pr" your real name?



The Java Ranch follows a policy regarding user names.
The main reasons why and a link how to change yours you'll find here:
http://www.javaranch.com/name.jsp


So, would you please change your user name before your next posting?
It will not affect anything you've already posted here. Just your user name will update.

Regards,
Bu.


all events occur in real time
Remko Strating
Ranch Hand

Joined: Dec 28, 2006
Posts: 893
A hint.

You're assigning and then the postfix operator will run.


Remko (My website)
SCJP 1.5, SCWCD 1.4, SCDJWS 1.4, SCBCD 1.5, ITIL(Manager), Prince2(Practitioner), Reading/ gaining experience for SCEA,
Clinton Magro
Greenhorn

Joined: May 16, 2007
Posts: 8
int i=0;
i=i++;
i=i++;





What Goes Around .. Comes Around
Prabhat Kumar
Greenhorn

Joined: Jan 25, 2007
Posts: 27
I could not get it!!
what do you mean when you say "too late"
i think the whole line will be executed before java goes to next line.in that case it should be 1 at the end of second line and then 2 at the end of 3rd line.
please explain
Prabhat
Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
Originally posted by Prabhat Kr:
what do you mean when you say "too late"
The sequence is crucial.

i=i++ is equivalent to:

int temp = i;
i = i + 1;
i = temp;
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: PostFix operator