0% found this document useful (0 votes)
10 views

02 Parameter

1) Java uses pass-by-value for primitive data types and pass-by-reference for object references. When a primitive type is passed to a method, the value is copied, but an object reference is passed by alias, meaning the reference points to the same object. 2) The example demonstrates that changing a primitive parameter does not affect the original value, but changing an object's properties inside the method does change the original object. 3) Parameters are initialized with the values of the arguments when the method is called, but changes made to primitive parameters have no effect outside the method, while changes to object states persist after method returns.

Uploaded by

kemii1704
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

02 Parameter

1) Java uses pass-by-value for primitive data types and pass-by-reference for object references. When a primitive type is passed to a method, the value is copied, but an object reference is passed by alias, meaning the reference points to the same object. 2) The example demonstrates that changing a primitive parameter does not affect the original value, but changing an object's properties inside the method does change the original object. 3) Parameters are initialized with the values of the arguments when the method is called, but changes made to primitive parameters have no effect outside the method, while changes to object states persist after method returns.

Uploaded by

kemii1704
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 14

Part 1:

Java as an Object-oriented
Programming Language

02. Parameter
PARAMETER
&
REFERENCE
Alias of Objects
 Two or more reference variables refer to the same object.
 Example:
ChessPiece bishop1 = new ChessPiece();
ChessPiece bishop2 = new ChessPiece();

bishop1 bishop2
Alias of Objects
Assignment of Reference Types

bishop2 = bishop1;

Before After

bishop1 bishop2 bishop1 bishop2


Assignment of Primitive Types

num2 = num1;

Before After

num1 num2 num1 num2

5 12 5 5
Parameter Passing

 Parameters in a Java method are passed by value


 The actual parameters (the values passed in) are
assigned to the formal parameters (declared in the
method header)
 For a parameter of primitive types, the formal parameter
is a copy of the actual parameter.
 For a parameter of reference types (objects), the formal
parameter is an alias of the actual parameter.
Example: Num.java
public class Num {
private int value;

public Num(int update) {


value = update;
}
public void setValue(int update) {
value = update;
}
public String toString() {
return value + "";
}
}
Example: ParameterTester.java
public class ParameterTester {
public void changeValues (int f1, Num f2, Num f3) {
System.out.println("Before changing the values:");
System.out.println("f1\tf2\tf3");
System.out.println(f1 + "\t" + f2 + "\t" +
f3 + "\n");
f1 = 999;
f2.setValue(888);
f3 = new Num (777);
System.out.println("After changing the values:");
System.out.println("f1\tf2\tf3");
System.out.println(f1 + "\t" + f2 + "\t" +
f3 + "\n");
}
}
Example: ParameterPassing.java
public class ParameterPassing {
public static void main (String[] args) {
ParameterTester tester = new ParameterTester();
int a1 = 111;
Num a2 = new Num (222);
Num a3 = new Num (333);
System.out.println("Before calling changeValues:");
System.out.println("a1\ta2\ta3");
System.out.println(a1 + "\t" + a2 + "\t" +
a3 + "\n");
tester.changeValues(a1, a2, a3);
System.out.println("After calling changeValues:");
System.out.println("a1\ta2\ta3");
System.out.println(a1 + "\t" + a2 + "\t" +
a3 + "\n");
}
}
Parameter tracing
Step 1

Before invoking changeValues

a1 a2 a3
111 222 333

f1 f2 f3

Step 2

tester.changeValues (a1, a2, a3);

a1 a2 a3
111 222 333

f1 f2 f3
= undefined
111
Parameter tracing
Step 3

f1 = 999;

a1 a2 a3
111 222 333

f1 f2 f3
999

Step 4

f2.setValue (888);

a1 a2 a3
111 888 333

f1 f2 f3
999
Parameter tracing
Step 5

f3 = new Num (777) ;

a1 a2 a3
111 888 333

f1 f2 f3
999 777

Step 6

After returning from changeValues

a1 a2 a3
111 888 333

f1 f2 f3
pass by value - pass by reference
data
variable objects
pass by value
Step 2
pass by reference
tester.changeValues (a1, a2, a3);

pass by reference
pass a1 a2 a3
by value
111 222 333

f1 f2 f3
111
Example: The Output

Before calling changeValues:


a1 a2 a3
111 222 333

Before changing the values:


f1 f2 f3
111 222 333

After changing the values:


f1 f2 f3
999 888 777

After calling changeValues:


a1 a2 a3
111 888 333

You might also like