0

Explain me this output using addOneTo?

public class MyClass { public static void main(String[ ] args) { int x = 5; System.out.println(addOneTo(x)); System.out.println(x); } static int addOneTo(int x) { x = x + 1; return x; } }

7th Jun 2020, 4:36 PM
Harsh Vyas
Harsh Vyas - avatar
2 Answers
0
The x in addOneTo() is a copy of the x in main(). Since Java uses pass by value, you just pass 5 to the method where it is incremented by 1 and returned. All this time your x in main() is untouched. So in the next line 5 gets printed.
7th Jun 2020, 4:55 PM
Avinesh
Avinesh - avatar
0
Ohk thnku
7th Jun 2020, 5:13 PM
Harsh Vyas
Harsh Vyas - avatar