Back to questions
JuniorTheoryCommon
What does the program print?
public class Main {
public static void main(String[] args) {
int[] a = {1, 2, 3};
int[] b = a;
int x = a[1];
b[0] = 9;
x = 7;
System.out.println(a[0] + " " + a[1]);
}
}9 2
- Expecting
1 2:int[] b = acopies only the reference, soaandblead to one array andb[0] = 9is seen througha. - Expecting
a[1]to become 7: an element ofint[]is anint, andint x = a[1]copies its value, not a link to the cell.
- How do you make
ban independent copy ofa? - What happens to the caller's array when a method writes to an element of its array parameter?