Java Argument Passing
In Java, there are two ways of argument passing. Call by value Call by reference Call by value Whenever we call a method and passes the value of the variable to the called method, it’s called call by value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | class Demo { void set(int i, int j) { i = i * 2; j = j / 2; } } public class Test { public static void main(String arg[]) { Demo obj = new Demo(); int a = 15, b = 20; System.out.println("Before call a=" + a + " b=" + b); obj.set(a, b); System.out.println("After call a="+a+" "+" b="+b); } } |
Result
1 2 | Before call a=15 b=20 After call a=15 b=20 |
Note: After multiplication and division value of a and b remain unchanged. … Read more