Skip to content

Commit 5c00e22

Browse files
committed
feat: Add ObjectPassingAB2 to contrast array mutation with primitive immutability in method calls
WHAT the code does: Defines ObjectPassingAB2 with two static methods: - change(int[] A, int index, int value) updates an array element at the specified index. - change2(int x, int value) assigns a new value to a primitive parameter, but only within the method scope. In main(): - Creates an array A = {2, 4, 6, 8, 10}. - Calls change(A, 2, 20) to update the third element from 6 to 20. - Prints the updated array using an enhanced for loop. - Declares primitive x = 10, calls change2(x, 20), and prints the result to show the original x remains unchanged. WHY this matters: Demonstrates Java’s pass-by-value semantics: - For arrays and objects, a reference copy is passed. Mutations affect the original object. - For primitives, only the raw value is passed. Reassignments inside the method affect only the local copy. Clarifies why Java is always “pass-by-value,” though outcomes differ for mutable vs immutable types. HOW it works: change(A, 2, 20) updates A[2] to 20. Printed result: 2 4 20 8 10. change2(x, 20) updates only the local copy of x inside the method. The original x in main remains 10. This demonstrates: ❌ Primitives cannot be changed inside methods, since only a value copy is passed. 📌 Arrays/objects → pass reference copy → can modify original. 📌 Primitives → pass value copy → original stays unchanged. Tips and gotchas: Be explicit about side effects when writing methods that mutate inputs, especially arrays or collections. For primitives, return the updated value if persistence outside the method is required. This distinction is critical in API design, where immutability and predictability matter. To avoid surprises, prefer functional-style programming where methods return new results rather than mutating inputs. Use-cases: Educational demonstration of Java parameter passing differences. Helps explain why wrapper classes like AtomicInteger exist to emulate reference-like behavior for primitives. Practical foundation for teaching defensive programming and immutability. Short key: class-objectpassingab2 array-mutation primitive-call-by-value. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent a4c46da commit 5c00e22

File tree

1 file changed

+6
-13
lines changed

1 file changed

+6
-13
lines changed
Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,22 @@
11
public class ObjectPassingAB2 {
2-
static void change(int A[], int index, int value)
3-
{
2+
static void change(int A[], int index, int value) {
43
A[index] = value;
54
}
6-
7-
static void change2(int x, int value)
8-
{
5+
static void change2(int x, int value) {
96
x = value;
107
}
11-
12-
public static void main(String[] args)
13-
{
8+
public static void main(String[] args) {
149
int A[] = {2, 4, 6, 8, 10};
1510

1611
change(A, 2, 20);
1712

18-
for (int x : A)
19-
{
20-
System.out.println(x);
13+
for (int x : A) {
14+
System.out.print(" "+x);
2115
}
2216

17+
System.out.println("\n");
2318
int x = 10;
24-
2519
change2(x, 20);
26-
2720
System.out.println("Value of primitive " + x);
2821
}
2922
}

0 commit comments

Comments
 (0)