+ 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

15th Apr 2018, 2:22 AM
Daniel Brum
4 Réponses
15th Apr 2018, 2:46 AM
Emma
+ 2
Daniel Brum Great to hear Daniel ☺
15th Apr 2018, 9:31 AM
Emma
+ 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
15th Apr 2018, 2:29 AM
TurtleShell
TurtleShell - avatar
+ 1
Thanks a lot, guys! Now I got it.
15th Apr 2018, 9:30 AM
Daniel Brum