aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes lost++ Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "lost++" Watch "lost++" New topic
Author

lost++

Anup Engineer
Ranch Hand

Joined: Mar 04, 2002
Posts: 48
Question from a mock:
What will happen when you attempt to compile and run the following code?
public class Inc{
public static void main(String argv[]){
Inc inc = new Inc();
int i =0;
inc.fermin(i);
i = i++;
System.out.println(i);
}
void fermin(int i){
i++;
}
}
1) Compile time error
2) Output of 2
3) Output of 1
4) Output of 0
Why would it print 0? I tested it. the correct ans is 4.
The line i=i++;
should take effect on the next line, isn't it?!
Thanks.


Anup Engineer
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
i=i++ will never increment the value of i...
Have a look at the following discussion:
http://www.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=24&t=014986


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: lost++