What is the output of the following code?
public class Main { public static void main(String args[]) { int s = 10; anotherMethod(s); System.out.println(s); someMethod(s); System.out.println(s); } static void someMethod(int val) { ++val; System.out.println(val); } static void anotherMethod(int val) { val = 20; System.out.println(val); } } a 20 10 11 11 b 20 20 11 10 c 20 10 11 10 d Compilation error
C
When primitive data types are passed to a method, the values of the variables in the calling method remain the same.
public class Main { public static void main(String args[]) { int s = 10;/*w w w . j a va 2 s .c o m*/ anotherMethod(s); System.out.println(s); someMethod(s); System.out.println(s); } static void someMethod(int val) { ++val; System.out.println(val); } static void anotherMethod(int val) { val = 20; System.out.println(val); } }
The code above generates the following result.