aspose file tools*
The moose likes Beginning Java and the fly likes about ++ Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "about ++" Watch "about ++" New topic
Author

about ++

zhang feng
Greenhorn

Joined: Dec 16, 2002
Posts: 19
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

Joined: Aug 24, 2001
Posts: 348
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;


not so smart guy still curious to learn new stuff every now and then
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
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.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
 
 
subject: about ++