Parameter passing mechanisms in programming languages - in general

  • Parameter passing = conveying some information (= parameter) to a method

  • The most commonly used parameter passing mechanisms are:

      • Pass-by- value
      • Pass-by-reference

What is passed (transfered) in the parameter passing mechanisms

  • Pre-requisite

      • The parameter variables are local variables to the method
        Arguments and parameter variables are different variables !!!

    Graphically:

    Repeat:   Arguments and parameter variables are different variables !!!

What is passed (transfered) in the parameter passing mechanisms

  • The pass-by-value mechanism:

      • The value of the argument is passed (= assigned) to the parameter variable

    Graphically:

    The method will use the copy of the argument's value in its computations

What is passed (transfered) in the parameter passing mechanisms

  • The pass-by-reference mechanism:

      • The address of the argument is passed (= assigned) to the parameter variable

    Graphically:

    The method will retrieve the value using the address in computations !

The parameter passing mechanism used by Java

  • Java always passes the arguments by-value to a method

  • However:

    • Because primitive typed variables and reference typed variables are stored differently:

      • The outcome of the pass-by-value mechanism is different for primitive typed variables and reference typed variables

  • Present8tion:

    1. First, I will show you the result of a method call using a primitive typed variable (int)

      And explain the result with diagrams

    2. Then, I will show you the result of a method call using a reference typed variable (an array)

      And explain the result with diagrams

The parameter passing mechanism used by Java   --- primitive data types

  • A primitive typed argument passed to a method cannot by modifed

  • Example program to show the parameter passing mechanism of Java:

       public class PassPrimitive
       {
          public static void Method1( int x ) // This x is local to Method1()
          {
       	 System.out.println( "Method1's parameter x before update = " + x );
       	 x = 4444;  // Update
       	 System.out.println( "Method1's parameter x after update  = " + x );
          }
        
          public static void main(String[] args)
          {
       	 int x;    // This x is local to main()
    
             x = 1 ;          
        
       	 System.out.println( "Main's x = " + x );
       	 Method1(x);   // Passes main's x to Method1's x
       	 System.out.println( "Main's x after Method1 update = " + x ); //  1 
          }
       }

DEMO: demo/02-review/PassPrimitive.java

What is going on inside the computer when a parameter is passed-by-value   --- primitive data types

  • Initialy, the program starts executing in the main( ) method:

     

What is going on inside the computer when a parameter is passed-by-value

  • The definition int x; will allocate (=create) the (local) variable x (on the stack):

     

What is going on inside the computer when a parameter is passed-by-value   --- primitive data types

  • The assignment x = 1; will update the main's variable x:

     

What is going on inside the computer when a parameter is passed-by-value

  • The method call Method1(x) will (1) allocate the parameter variable x of Method1( ):

     

What is going on inside the computer when a parameter is passed-by-value   --- primitive data types

  • ... (2) then pass (= assign) main's argument x to Method1's parameter variable x:

    The method Method1( ) starts execution....

What is going on inside the computer when a parameter is passed-by-value

  • The assignment x = 4444; will update Method1's parameter variable x:

    Notice that main's local variable x is unchanged !

What is going on inside the computer when a parameter is passed-by-value   --- primitive data types

  • The method Method1( ) returns and the parameter variable x is de-allocated:

    The method main( ) will resume execution...

What is going on inside the computer when a parameter is passed-by-value   --- primitive data types

  • When main( ) continues, its variable x is unchanged after the method invocation:

    Summary:   primitive type arguments ine Java can not be changed by a method call

The parameter passing mechanism used by Java   --- reference data types

  • A reference typed argument passed to a method will by modifed

  • Example program to show the parameter passing mechanism of Java:

       public class PassPrimitive
       {
          public static void Method1( int x ) // This x is local to Method1()
          {
       	 System.out.println( "Method1: x[1] before update = " + x[1] );
       	 x[1] = 4444;  // Update
       	 System.out.println( "Method1: x[1] after update  = " + x[1] );
          }
        
          public static void main(String[] args)
          {
       	 int[] x = {1, 2, 3, 4};  // x local var of main()
                                      // x references to array {1, 2, 3, 4}
    
             System.out.println( "Main, before call: x[] = " + Arrays.toString(x) );
       	 Method1(x);   // Passes main's x to Method1's x
             System.out.println( "Main, after  call: x[] = " + Arrays.toString(x) );
          }
       }

DEMO: demo/02-review/PassReference.java

What is going on inside the computer when a parameter is passed-by-value   --- reference data types

  • Initialy, the program starts executing in the main( ) method:

     

What is going on inside the computer when a parameter is passed-by-value

  • The definition int[] x = {1,2,3,4}; will make x reference to an array:

    The (local) variable x contains the reference (= address) of the array

What is going on inside the computer when a parameter is passed-by-value

  • The method call Method1(x) will (1) allocate the parameter variable x of Method1( ):

     

What is going on inside the computer when a parameter is passed-by-value   --- reference data types

  • ... (2) then pass (= assign) main's argument x to Method1's parameter variable x:

    Notice that: the parameter variable x reference to the same array !

What is going on inside the computer when a parameter is passed-by-value

  • The assignment x[1] = 4444; will update the 2nd element in the array:

    Notice that main's array variable is changed !

What is going on inside the computer when a parameter is passed-by-value   --- reference data types

  • The method Method1( ) returns and the parameter variable x is de-allocated:

    The method main( ) will resume execution...

What is going on inside the computer when a parameter is passed-by-value   --- reference data types

  • When main( ) continues, its array x is changed after the method invocation:

    Summary:   reference type arguments in Java can be changed by a method call