0
What is output of following code ?and why ?
public class Program { public static void main(String[] args) { int test = 5; test = test++; System.out.println(test); } }
3 Answers
+ 4
Because at first "test" will be evaluated (5) and this value will be used later by the equation operator. After that value of test will be increased to 6 by the ++ operator. But this new value won't be used, test value will be overwritten by the stored 5.
+ 1
int test = 5; // setting test to 5
test = test++; // setting test to 5.
System.out.println(test); // output is 5
test = ++test; would output 6.
0
test ++ will make it 6 for the next function or loop