Analyze the code below public class Test public static void
Analyze the code below:
public class Test {
public static void main(String[] args) {
int[] x = {1, 2, 3, 4};
int[] y = x;
x = new int[2];
System.out.print(y[2] + \" \");
}
}
java
Solution
Answer:
Its output will be 3.
Y is copied to x in second statement.\" X=new int[2] \"this statement saves space to variable x, but has no values in it.For y[2] is 3rd element of array which was copied at first. Hence 3
![Analyze the code below: public class Test { public static void main(String[] args) { int[] x = {1, 2, 3, 4}; int[] y = x; x = new int[2]; System.out.print(y[2] Analyze the code below: public class Test { public static void main(String[] args) { int[] x = {1, 2, 3, 4}; int[] y = x; x = new int[2]; System.out.print(y[2]](/WebImages/32/analyze-the-code-below-public-class-test-public-static-void-1094262-1761576718-0.webp)