0
tutorial problem
In the sololearn tutorial for the value and reference types, i come across this question "What is the output of this code? public static void main(String[ ] args) { int x = 4; square(x); System.out.println(x); } static void square(int x) { x = x*x; }" witch simply boils down to 4^2, witch is 16. the problem is that the area where you input the answer only outputs one character. encoding it in hex doesen't work either. the "right" answer is 4. is there something i'm missing or is the creator of the tutorial that oblivious?
2 Respuestas
+ 1
the answer is 4 as the function modifies x only locally and does not return the modified value to main.
so as you print x from the main it is still 4.
only if you had the print within the function it would output 16.
0
4