Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

Module2-CC103

This document explains the parameter passing mechanisms in Java, focusing on the differences between pass-by-value and pass-by-reference. It clarifies that Java uses pass-by-value for both primitive types and objects, where primitives are copied and objects are passed as references to their memory addresses. The document also includes examples, misconceptions, and practical exercises to reinforce understanding of these concepts.

Uploaded by

arjaysoberano36
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Module2-CC103

This document explains the parameter passing mechanisms in Java, focusing on the differences between pass-by-value and pass-by-reference. It clarifies that Java uses pass-by-value for both primitive types and objects, where primitives are copied and objects are passed as references to their memory addresses. The document also includes examples, misconceptions, and practical exercises to reinforce understanding of these concepts.

Uploaded by

arjaysoberano36
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Module 2 – Parameter passing mechanism

Parameter Passing Mechanism

2.0 Learning Objectives:


 Differentiate pass-by-reference from pass-by-value in Java, and choose the
appropriate parameter passing mechanism when defining and invoking methods.
 Apply correct coding style on the formulated solutions.

2.1 What are Parameter Passing Mechanisms?


When a method is called in Java, values (arguments) are passed to it from the calling
method. The way these arguments are passed is known as the parameter passing
mechanism.

2.2 Parameter Passing Mechanisms:

1. Pass by Value
2. Pass by Reference

2.3 Java and Parameter Passing:

In Java, all arguments are passed by value. However, how this works depends on the
type of the parameter:

1. Primitive data types (e.g., int, double) are passed by value.


2. Objects are passed by value, but the value is a reference to the object. This can
sometimes give the illusion of "pass by reference."

2.3.1 Pass by Value

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.

Example with Primitive Types:

public class PassByValueExample {


public static void main(String[] args) {
int number = 10;
System.out.println("Before calling the method: " + number);
modifyValue(number);
System.out.println("After calling the method: " + number);
}

public static void modifyValue(int num) {


num = 20; // Modifies the copy of the value
System.out.println("Inside the method: " + num);
}
}
Output:

Before calling the method: 10


Inside the method: 20
After calling the method: 10

Explanation:

 A copy of the variable number is passed to the modifyValue method.


 Modifying num inside the method does not affect the original number variable.

2. Pass by Reference (Not Supported in Java)

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.

Why Java Is Not Pass by Reference:

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.

Example with Objects:


class Person {
String name;
}

public class PassByReferenceExample {


public static void main(String[] args) {
Person person = new Person();
person.name = "John";
System.out.println("Before calling the method: " + person.name);
modifyObject(person);
System.out.println("After calling the method: " + person.name);
}

public static void modifyObject(Person p) {


p.name = "Alice"; // Modifies the object's property
}
}

Output:

Before calling the method: John


After calling the method: Alice

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

A copy of the value is passed. A reference to the original value is passed.

Changes do not affect the


Changes affect the original.
original.

Not supported in Java (objects use pass by value of


Used for primitive types in Java.
references).

Practical Example to Demonstrate Both:


class Example {
int value;
}

public class ParameterPassingDemo {


public static void main(String[] args) {
int primitive = 5;
Example obj = new Example();
obj.value = 10;

modifyPrimitive(primitive);
modifyObject(obj);

System.out.println("Primitive after method: " + primitive); // 5


System.out.println("Object value after method: " + obj.value); // 20
}

public static void modifyPrimitive(int num) {


num = 50; // No effect on original variable
}

public static void modifyObject(Example obj) {


obj.value = 20; // Modifies the original object's field
}
}

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.

1. Which of the following statements is true about parameter passing in Java?


A) Java uses pass-by-reference for primitive data types.
B) Java uses pass-by-value for primitive data types.
C) Java uses pass-by-reference for objects.
D) Java uses pass-by-reference for both primitive data types and objects.

2. What happens when you pass an object to a method in Java?


A) The method gets a copy of the object and can modify it.
B) The method receives a reference to the original object and can modify it.
C) The method receives a copy of the object but cannot modify its fields.
D) The method cannot modify the object.

3. Which of the following code snippets demonstrates the correct way to pass an array
to a method in Java?

A) public void modifyArray(int arr) {

arr = 10;
}

B) public void modifyArray(int[] arr) {

arr[0] = 10;
}

C) public void modifyArray(int arr[]) {

arr[0] = 10;
}

D) public void modifyArray(int arr[]) {

arr = new int[5];


}

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.

9. Consider the following method:

public void swap(int a, int b) {


int temp = a;
a = b;
b = temp;
}

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.

2.4.2 Programming Exercises:


1. Write a Java program that demonstrates pass by value using primitive data types.
Create a method modifyValue(int num) that takes an integer parameter, modifies its
value inside the method, and prints the original and modified values in the main()
method.

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:

Before modification: John


After modification: Mike

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:

Original Array: [1, 2, 3]


Modified Array: [2, 4, 6]

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

You might also like