+ 1
About the code in the "Value Types" lesson in Java
Well, I guess there's something wrong in this code, because it always prints 5. I dunno how to explain it, but it's like there's something missing in the first part. Can anyone help me? https://code.sololearn.com/cVDxGbXsRRH2/?ref=app
4 ответов
+ 2
Here's a solution for you:
https://code.sololearn.com/cAwd2Mk5Fk56/?ref=app
+ 2
Daniel Brum Great to hear Daniel ☺
+ 1
Problem is that arguments get passed by VALUE the actual variable (x) is untouched, x’s VALUE gets put into num so your function is quite useless, a way to fix this would be:
public static int addOne(int num) {
return ++num;
}
x = addOne(x); // new value will be 6
+ 1
Thanks a lot, guys! Now I got it.