Java Reference
In-Depth Information
See ยง 9.6.3.4 for another situation where the difference between public and non- public
methods of Object requires special care.
Example 10.7-1. Arrays Are Cloneable
Click here to view code image
class Test1 {
public static void main(String[] args) {
int ia1[] = { 1, 2 };
int ia2[] = ia1.clone();
System.out.print((ia1 == ia2) + " ");
ia1[1]++;
System.out.println(ia2[1]);
}
}
This program produces the output:
false 2
showing that the components of the arrays referenced by ia1 and ia2 are different vari-
ables.
Example 10.7-2. Shared Subarrays After A Clone
The fact that subarrays are shared when a multidimensional array is cloned is shown
by this program:
Click here to view code image
class Test2 {
public static void main(String[] args) throws Throwable {
int ia[][] = { {1,2}, null };
int ja[][] = ia.clone();
System.out.print((ia == ja) + " ");
System.out.println(ia[0] == ja[0] && ia[1] == ja[1]);
}
}
This program produces the output:
false true
showing that the int[] array that is ia[0] and the int[] array that is ja[0] are the same array.
Search WWH ::




Custom Search