Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

PostFix operator

 
Prabhat Kumar
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Remko Strating
Ranch Hand
Posts: 893
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A hint.

You're assigning and then the postfix operator will run.
 
Clinton Magro
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int i=0;
i=i++;
i=i++;



 
Prabhat Kumar
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic