What is output of following code ?and why ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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); } }

14th Feb 2017, 6:41 AM
Vishal Magar
Vishal Magar - avatar
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.
14th Feb 2017, 7:13 AM
Tamás Barta
Tamás Barta - avatar
+ 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.
14th Feb 2017, 6:57 AM
Michael W
Michael W - avatar
0
test ++ will make it 6 for the next function or loop
14th Feb 2017, 7:41 AM
Sivalal Ramakrishna
Sivalal Ramakrishna - avatar