Home » java » Java Argument Passing

Java Argument Passing

In Java, there are two ways of argument passing.

  1. Call by value
  2. 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.   

Result

Note: After multiplication and division value of a and b remain unchanged.

Explanation : In the above program, we are passing the value of a and b 15 & 20 respectively and  set the value of i and j. After setting the value of i and j value of variable a and b remain unchanged because modifications occurs in variable i and j.   

Call by reference

Whenever we call a method and passes the reference of the variable to the called method, it’s called call by reference.   

Explanation:  In the above program, in the  lineobj.set(obj1);               “ we are passing the object obj to the method “set (Demo  o)” and reference “o “ of Demo class will also refer to object obj.

Call by reference

In the above figure

Explanation: In the above program, when “Demo obj =  new   Demo(10,20); “ will execute it will pass the value of a and b 10 & 20 respectively for object obj.

And when “obj.set(obj);” will execute it will pass object obj and reference of class Demo “o” refer to Explanation: “obj”. Reference “o” will modify/update the value of object obj.

Managed by MochaHost - Fast & Easy WordPress Hosting