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
What is
passed (transfered) in the
parameter passing mechanisms
What is
passed (transfered) in the
parameter passing mechanisms
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:
- First, I will show you the
result of
a method call using
a
primitive typed
variable (int)
And explain the
result with
diagrams
- 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
What is going on inside the computer when a
parameter is passed-by-value
What is going on inside the computer when a
parameter is passed-by-value
--- primitive data types
What is going on inside the computer when a
parameter is passed-by-value
--- primitive data types
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
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
What is going on inside the computer when a
parameter is passed-by-value
What is going on inside the computer when a
parameter is passed-by-value
--- reference data types
What is going on inside the computer when a
parameter is passed-by-value
--- reference data types
❮
❯