Skip to content

Commit a4c46da

Browse files
committed
feat: Add ObjectPassingAB to demonstrate object reference vs primitive parameter passing
WHAT the code does: Defines a ObjectPassingAB class with two static methods: - change(int[] X, int index, int value) updates an array element at the given index. - change2(int x, int value) assigns a new value to a primitive, but only within local scope. In main(): - Creates an integer array A = {2, 4, 6, 8, 10}. - Calls change(A, 2, 20) to update the 3rd element of A from 6 to 20. - Prints the updated array elements using an enhanced for loop. WHY this matters: Illustrates the difference between Java parameter passing for objects vs primitives: - Arrays are objects in Java, so their references are passed by value. Mutating the array inside a method updates the caller’s array. - Primitives are passed by value, and reassigning them inside a method does not affect the caller. Demonstrates a common source of confusion and a key concept in understanding Java’s “pass-by-value for all types” model. HOW it works: change(A, 2, 20) receives the array reference and modifies A[2]. Because arrays are mutable, the modification is visible in main(). Final output is: 2 4 20 8 10. change2() is defined but unused here; if called, it would not affect the caller’s variable. Tips and gotchas: All arguments in Java are passed by value, but for objects the value is a reference, allowing indirect mutation. To prevent side effects, create defensive copies of arrays or collections before modifying them inside methods. change2() shows why primitives must be returned if you want to persist updates outside the method. Be cautious when designing APIs: modifying inputs can be surprising to callers. Use-cases: Educational example for teaching parameter passing and mutability in Java. Foundation for understanding side effects when working with arrays and collections. Practical when designing utility methods that intentionally mutate array data. Short key: class-objectpassingab array-mutation primitive-vs-reference. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent add8a75 commit a4c46da

File tree

1 file changed

+7
-12
lines changed

1 file changed

+7
-12
lines changed
Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
1-
public class ObjectPassingAB
2-
{
3-
static void change(int X[], int index, int value)
4-
{
1+
public class ObjectPassingAB {
2+
static void change(int X[], int index, int value) {
53
X[index]=value;
64
}
7-
8-
static void change2(int x, int value)
9-
{
5+
static void change2(int x, int value) {
106
x=value;
117
}
12-
public static void main(String[] args)
13-
{
8+
public static void main(String[] args) {
149
int A[]={2,4,6,8,10};
1510
change(A,2,20);
1611

17-
for(int x:A) //Acessing all elements of A and print it.
12+
for(int x:A) //Accessing all elements of A and print it.
1813
{
19-
System.out.println(x);
14+
System.out.print(" "+x);
2015
}
2116
}
22-
}
17+
}

0 commit comments

Comments
 (0)