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; } }
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.
0
Ohk thnku