Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strings in One Shot | Java Lecture 49 #1

Open
khanra17 opened this issue Apr 17, 2023 · 0 comments
Open

Strings in One Shot | Java Lecture 49 #1

khanra17 opened this issue Apr 17, 2023 · 0 comments

Comments

@khanra17
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant