by Dinesh Thakur

There are mainly two ways of passing arguments to methods:

• Pass by value

• Pass by reference

Java directly supports passing by value; however, passing by reference will be accessible through reference objects.

Pass by value

When the arguments are passed using the pass by value mechanism, only a copy of the variables are passed which has the scope within the method which receives the copy of these variables. The changes made to the parameters inside the methods are not returned to the calling method. This ensures that the value of the variables in the calling method will remain unchanged after return from the calling method.

Pass by value.

import java.io.*;

public class Sum

{

       static int CaiculateTotal(int n1)

      {

           int total=0;

           int marks[] = new int[3]:

           try

           {

                        BufferedReader br= newBufferedReader( new                          InputStreamReader(System.in));

                 for(int i=0; i<n 1; i++)

                   {

                  marks[i]= Integer.parselnt(br.readLine());

                  total+=marks[i];

                   }

           }

            catch(Exception e)

           {

                     System.out.println("Array out of range");

            }

                   return total;

     }

           public static void main(String args[]) throws IOException

         {

         int n,Max;

          System.out.println("Enter the numbers :");

          Max = CalculateTotal(3);

          System.out.println("Sum of the numbers is:" +Max);

         }

}

The output of Program is as shown below:

Enter the numbers:

2

3

4

Sum of the numbers is: 9

In Program the value 3 has been passed to the method Calculate Total. The argument n1 of CalculateTotal method takes its value as 3 and performs the rest of the execution of program.

Pass by reference

In the pass by reference mechanism, when parameters are passed to the methods, the calling method returns the changed value of the variables to the called method. The call by reference mechanism is not used by Java for passing parameters to the methods. Rather it manipulates objects by reference and hence all object variables are referenced. Program illustrates the call by reference mechanism in Java.

Program to illustrate pass by reference.

import java.io.*;

class swap_ref

{

        static void swap(First ob)

        {

                     int temp;

                     temp=ob.a;

                     ob.a=ob.b;

                     ob.b=temp;

          }

                     public static void main(String args[ ])

                     {

                          First ob=new First(10,20);

                          System.out.println("Before SWAP");

                          System.out.println("a=" +ob.a+"b=" +ob.b);

                           swap(ob);

                          System.out.println("After SWAP");

                          System.out.println ("a="+ob.a +"b=" +ob.b);

                     }

}

         class First

          {

                    int a;

                    int b;

                  First(int x, int y)

                        {

                                   a = x;

                                b = y;

                         }

         }

The output of Program is as shown below:

Before SWAP

a=10 b=20

After SWAP

a= 20 b=10

Here, the method swap is invoked with object ob as parameter, and any manipulation of this object within the method will affect the original objects. Thus, the values of a and bare interchanged after method swap is called.