public class Test {
public static void main(String[] args) {
int[] a = {1, 2, 3, 4, 5};
int[] b = a;
int[] c = {6, 7, 8};
a = c;
for(int i = 0; i < a.length; i ++)
System.out.print(a[i] + " ");
System.out.print("\n");
for(int i = 0; i < b.length; i ++)
System.out.print(b[i] + " ");
System.out.print("\n");
}
}
I have initialized array a and assigning reference of a to new array b. Now I initialized a new array c and passed its reference to array a. Since array b is reference to array a, b should have new values that are in c but b is having old values of a. What is the reason behind it? Output is given below -
Output -
6 7 8
1 2 3 4 5