0
public class Q3 {
public static void doSomething(int [] x, int[] y) {
    for (int i = 0; i < x.length; i += 2) {
        x[i] = y[i];
    }
    y = x;
}
public static void main(String[] args) {
    int [] x = {2, 4, 6, 8};
    int [] y = {0, 1, 2, 3};
    doSomething(x, y);
    System.out.println(x[0] + "," + x[1] + "," + x[2] + "," + x[3] );
    System.out.println(y[0] + "," + y[1] + "," + y[2] + "," + y[3] );
    }
}
  1. this is a question about array function why answer is 0,4,2,8/n 0,1,2,3 rather than 0,4,2,8/n 0,4,2,8
Novice
  • 1
  • 2
  • `y = x` only changes the references locally in the doSomething method, it doesn't affect the instances, nor the references in the calling main method. – Adriaan Koster Jun 03 '22 at 08:51

0 Answers0