Module2-CC103
Module2-CC103
1. Pass by Value
2. Pass by Reference
In Java, all arguments are passed by value. However, how this works depends on the
type of the parameter:
In pass by value, a copy of the value is passed to the method. This means any changes
made to the parameter inside the method do not affect the original value.
Explanation:
In pass by reference, a reference to the original variable is passed to the method. This
means any changes made to the parameter inside the method directly affect the
original object.
In Java, objects are passed by value, but the value passed is a reference (memory
address) to the object. As a result, the method can manipulate the object’s properties,
but it cannot reassign the original reference.
Output:
Explanation:
A copy of the reference to the object person is passed to the modifyObject method.
The method uses this reference to modify the name property of the object, which is
reflected in the original object.
Misconception:
The reference itself cannot be changed to point to a new object inside the method. This
demonstrates that Java does not support true pass by reference.
1
Example:
public static void modifyReference(Person p) {
p = new Person(); // Reassigning the reference
p.name = "Bob"; // Modifies the new object
}
In this case, the original object remains unaffected because the new reference p is a
local copy.
Key Differences:
Pass by Value Pass by Reference
modifyPrimitive(primitive);
modifyObject(obj);
Summary:
1. Java always uses pass by value for both primitive and object types.
2. For primitives, the value itself is copied.
3. For objects, the reference to the object is copied, allowing changes to the object’s fields
but not the reference itself.
2
2.4 Learning Activities
2.4.1 Multiple Choice:
Directions: Read each question carefully. Ensure you understand the question before
selecting an answer.
3. Which of the following code snippets demonstrates the correct way to pass an array
to a method in Java?
arr = 10;
}
arr[0] = 10;
}
arr[0] = 10;
}
4. What is the result of modifying the value of a primitive type parameter inside a
method in Java?
A) The original value in the calling method will change.
B) The value is copied and the original value remains unchanged.
C) The original value is overwritten in the calling method.
D) The compiler will throw an error.
5. What happens when a method accepts a wrapper class object, like Integer or Double,
and the value is modified inside the method?
A) The original value in the calling method will change.
B) The original value in the calling method remains unchanged.
C) The compiler will throw a runtime exception.
D) The object itself will be modified in the calling method.
3
6. What is the result of passing an array to a method in Java?
A) A copy of the array is passed, and changes inside the method do not affect the
original array.
B) A reference to the original array is passed, and changes inside the method affect the
original array.
C) The array cannot be modified inside the method.
D) A new array is created inside the method and replaces the original array.
7. In Java, if a method receives an object and modifies its attributes, what happens to
the object outside the method?
A) The object is copied, and the original object is unaffected.
B) The object is passed by reference, and the original object is modified.
C) The object is passed by value, so changes inside the method do not affect the
original object.
D) The object is destroyed after the method finishes.
8. Which of the following is correct about the parameter passing mechanism in Java?
A) Java always uses pass-by-reference for both primitive types and objects.
B) Java uses pass-by-value for both primitive types and objects.
C) Java uses pass-by-value for primitive types and pass-by-reference for objects.
D) Java uses pass-by-reference for primitive types and pass-by-value for objects.
What will happen if the swap method is called in the main method with two integers x =
5 and y = 10?
A) The values of x and y will be swapped inside the swap method and in the main
method.
B) The values of x and y will be swapped inside the swap method, but the main method
will not reflect the change.
C) The values of x and y will not be swapped at all.
D) The compiler will throw an error.
10. Which of the following is true when passing an object of a custom class to a method
in Java?
A) The method receives a copy of the object and can modify it.
B) The method receives a reference to the object, and changes to its attributes will
reflect outside the method.
C) The method cannot modify the object in any way.
D) The object is passed by value, so changes inside the method do not affect the
original object.
4
Expected Output:
Original Value: 10
Modified Value: 20
2. Write a Java program that demonstrates pass by reference using objects. Create a
class Student with a field name and a method changeName(String newName). In the
main() method, create a Student object, pass it to a method modifyStudent(Student
s), and change the name inside the method. Print the name before and after the
modification.
Expected Output:
3. Write a Java program that demonstrates pass by value using an array. Create a
method modifyArray(int[] arr) that accepts an integer array, doubles an element
inside the array, and prints the original array and modified array in the main() method.
Expected Output:
4. Write a Java program to swap two numbers using method arguments. Create a
method swap(int a, int b) that swaps the values of a and b. Print the swapped values
inside the swap method. The main() method should print the numbers before and after
calling the swap() method.
Expected Output:
Before Swap: a = 5, b = 10
After Swap: ?
2.5 References
Ebook - Farrel, J, Java Programming, Course Technology, Cengage Learning
• https://www.geeksforgeeks.org/parameter-passing-techniques-in-java-with-examples/
• https://www.javatpoint.com/parameter-passing-techniques-in-java-with-examples