0

Can someone give me brief description of how this example works

I am a beginner in programming world. I was going through the java course here, at this point I got stuck because I don't know how the output of the following example is 5. Can someone please explain to me how this example actually works? public class MyClass { public static void main(String[ ] args) { int x = 5; addOneTo(x); System.out.println(x); } static void addOneTo(int num) { num = num + 1; } } // Outputs "5"

16th Aug 2018, 12:00 PM
Midhat shah
Midhat shah - avatar
2 Respostas
+ 3
It is happened because you pass copy of varible x in your method. Therefore initial value is not changed
16th Aug 2018, 12:36 PM
Roman Khristoforov
Roman Khristoforov - avatar
+ 3
I had tried my best to explain it here. Hope this helps☺️☺️. https://www.sololearn.com/Discuss/1406887/?ref=app
16th Aug 2018, 1:23 PM
Meet Mehta
Meet Mehta - avatar