Skip to content

Commit 9c3dcbf

Browse files
committed
feat: Modify original array by updating first element through method reference
🧠 Logic: - Arrays in Java are objects and passed **by reference**. - `nums[0] = 99;` changes the original array's first element. - Method `change()` modifies `arr` in-place. Signed-off-by: Somesh diwan <[email protected]>
1 parent 80f94e2 commit 9c3dcbf

File tree

1 file changed

+1
-3
lines changed

1 file changed

+1
-3
lines changed

Section10Methods/Methods 2.O/src/ChangeValue.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22

33
public class ChangeValue{
44
public static void main(String[] args) {
5-
// create an array
65
int[] arr = {1, 3, 2, 45, 6};
76
change(arr);
87
System.out.println(Arrays.toString(arr));
98
}
10-
119
static void change(int[] nums) {
12-
nums[0] = 99; // if you make a change to the object via this ref variable, same object will be changed
10+
nums[0] = 99; // if you make a change to the object via this ref variable, the same object will be changed
1311
}
1412
}

0 commit comments

Comments
 (0)