PW-Skills-Java-Course-Codes icon indicating copy to clipboard operation
PW-Skills-Java-Course-Codes copied to clipboard

Strings in One Shot | Java Lecture 49

Open khanra17 opened this issue 2 years ago • 0 comments

In the Lecture 49: Time: 1:00:00 Based on the code you provided, the output should be 1 and not 4. The reason for this is that when you create an array in Java, it creates a new object in memory to store the values in the array. So, when you assign arr to {1, 2, 3} and brr to {1, 2, 3}, you have two separate arrays in memory with the same values.

When you change the value of arr[0] to 4, it only affects the arr array and not the brr array. Therefore, when you print brr[0], it should still be 1.

image

However, if you had assigned brr to arr like this:

int[] arr = {1, 2, 3};
int[] brr = arr;
arr[0] = 4;
System.out.println(brr[0]);

then the output would be 4 because both arr and brr are pointing to the same array in memory, so when you change the value of arr[0], it also changes the value of brr[0].

image

khanra17 avatar Apr 17 '23 07:04 khanra17