Can somebody tell me why the value returned is 3 and not 8. Doesn\'t the return x
statement from the addFive
method change the value of x
The value in the main function is completely different from the value in the addFive(int x )
function. You send an x
from main to addFive(int x)
method.
JVM makes a copy of x and send it to addFive(int x)
method. Then x
changes in the addFive(int x)
method. But the x
in main()
method remains unchanged.
If you want to get the changed value returned by addFive(int x)
from main method you can do the following -
int returnedValueFromAddFive = addFive(x)
Hope it will help.
Thanks a lot.