Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

about ++

 
zhang feng
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have a class

i dont understand , c++ will print 1; java print 0;
java development from c++ ,why the statement(i=i++)
print 0,and not 1;
 
chi Lin
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there is some discussion here.
Originally posted by zhang feng:
i have a class

i dont understand , c++ will print 1; java print 0;
java development from c++ ,why the statement(i=i++)
print 0,and not 1;
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This discussion comes up a lot:
http://www.coderanch.com/t/242161/java-programmer-SCJP/certification/Unexpected-output
The two things to note are:
1) The behavior of this is undefined in C++. You may see one result in one compiler and another result in another compiler.
2) The postfix operator returns a result and then increments. So when you do i = i++, if i = 0 then you are getting:
a) return 0
b) increment i to 1
c) assign the returned value of 0 to i
Operator precedence says that postfix occurs before assignment.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic