• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

lost++

 
Anup Engineer
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic