Wednesday, 18 April 2012

diffrence between Pass By Reference and Pass By Value


Pass By Reference :
In Pass by reference address of the variable is passed to a function. Whatever changes made to the formal parameter will affect to the actual parameters
- Same memory location is used for both variables.(Formal and Actual)-
- it is useful when you required to return more then 1 values

Pass By Value:
- In this method value of the variable is passed. Changes made to formal will not affect the actual parameters.
- Different memory locations will be created for both variables.
- Here there will be temporary variable created in the function stack which does not affect the original variable.


here two ways can passes an argument to subroutine (method).
1)call-by-value
2)call-by-reference

call-by-value:
----------------
In this process first method copies the value of an argument in to formal parameters of the subroutine. Therefore any changes made to the parameter of the subroutine will have no effect to argument used to call it.

call-by-reference:
---------------------
In this process method copies the value of an argument in to the formal parameter of the subroutine. Therefore any changes made to the parameter of the subroutine will have effect to the arguments used to call it.

Most methods passed arguments when they are called. An argument may be a constant or a variable. For example in the expression: Math.sqrt(x); The variable x is passed here.

Pass by Reference means the passing the address itself rather than passing the value and pass by value means passing a copy of the value as an argument.

This is simple enough, however there is an important but simple principle at work here. If a variable is passed, the method receives a copy of the variable's value. The value of the original variable cannot be changed within the method. This seems reasonable because the method only has a copy of the value; it does not have access to the original variable. This process is called pass by value.

However, if the variable passed is an object, then the effect is different. We often say things like, "this method returns an object ...", or "this method is passed an object as an argument ..." But this is not quite true, more precisely, we should say, something like "this method returns a reference to an object ..." or "this method is passed a reference to an object as an argument ..."

Generally, objects are never passed to methods or returned by methods. It is always "a reference to an object" that is passed or returned. In general, pass by value refers to passing a constant or a variable holding a primitive data type to a method, and pass by reference refers to passing an object variable to a method. In both cases a copy of the variable is passed to the method. It is a copy of the "value" for a primitive data type variable; it is a copy of the "reference" for an object variable. So, a method receiving an object variable as an argument receives a copy of the reference to the original object.

Here's the clincher: If the method uses that reference to make changes to the object, then the original object is changed. This is reasonable because both the original reference and the copy of the reference "refer to" to same thing — the original object. There is one exception: strings. Since String objects are immutable in Java, a method that is passed a reference to a String object cannot change the original object.

To understand pass by reference lets see the sample program below:

public class TestPassByReference {
         public static void main(String[] args) {
                // declare and initialize variables and objects
                int i = 25;
                String s = "Java is fun!";
                StringBuffer sb = new StringBuffer("Hello, world");

                // print variable i and objects s and sb
                System.out.println(i);     // print it (1)
                System.out.println(s);    // print it (2)
                System.out.println(sb);  // print it (3)

                // attempt to change i, s, and sb using methods
                iMethod(i);
                sMethod(s);
                sbMethod(sb);

                 // print variable i and objects s and sb (again)
                 System.out.println(i);    // print it (7)
                 System.out.println(s);   // print it (8)
                 System.out.println(sb); // print it (9)

         }

         public static void iMethod(int iTest) {
                iTest = 9;                          // change it
                System.out.println(iTest); // print it (4)
                return;
         }

         public static void sMethod(String sTest) {
                sTest = sTest.substring(8, 11); // change it
                System.out.println(sTest);        // print it (5)
                return;
         }

         public static void sbMethod(StringBuffer sbTest) {
                sbTest = sbTest.insert(7, "Java "); // change it
                System.out.println(sbTest);            // print it (6)
                return;
          }
}

Output of the program :

25
Java is fun!
Hello, world
9
fun
Hello, Java world
25
Java is fun!
Hello, Java world


TestPassByReference begins by declaring and initializing three variables: an int variable named i, a String object variable named s, and a StringBuffer object variable named sb. The values are then printed. Then, each variable is passed as an argument to a method. Within each method, the copy of the variable exists as a local variable. The value of the variable — or the value of the object referred to by the variable, in the case of the String and StringBuffer object variables — is changed and printed within each method. The print statements are numbered to show the order of printing. Back in the main() method, the three values are printed again. Have a look at the output and see if it is consistent with our previous discu


No comments:

Post a Comment