|
Arrays are objects. However, the conversion rules for arrays are bit different from the rest of the object reference conversions. Arrays can store both primitives as well as references as their elements. Following rules address arrays of both kinds:
1. In case of array of primitives, an array-type can be converted only to the java.lang.Object class-type or the Serializable and Cloneable interface-types.
The built-in arrays in Java are objects of array class. This class has the Object class as its superclass and it implements two interfaces Serializable and Cloneable. Hence, you can assign its reference to any one of these three types. For example, following code assigns the int[] reference to variables of Object class-type and two interface-types.
int[] intArray = new int[5];
java.io.Serializable aSerializableObj = intArray; //Valid
Cloneable aCloneableObj = intArray; //Valid
Object anObject = intArray; //Valid
Note that primitive conversion rules do not play any role in array reference conversions. For instance, you cannot assign int[] reference to an long[] reference just because long is wider than int. Following code will not compile.
long[] longArray = intArray; // Error!
If you assign an array reference to another array reference, they must be of same primitive type. For instance, you can assign an int array reference to another array reference of only int[] type.
2. In case of array of object references, an array-type can be converted to the java.lang.Object class-type or the Serializable and Cloneable interface-types.
All array objects can be assigned to Object type as well as the Serializable and Cloneable interface-types. The array of object references is no exception. The following code creates an array of checkingAccounts. It is assigned to the variables of type Object, Serializable and Cloneable as:
CheckingAccount[] checkingAccounts = new CheckingAccount[5];
java.io.Serializable aSerializableObj = checkingAccounts; //Valid!
Cloneable aCloneableObj = checkingAccounts; //Valid!
Object anObject = checkingAccounts; //Valid!
3. An array of object references can be converted into a new array of object references provided the elements of that array are assignment-compatible (convertible) with the elements of new array.
The widening reference conversions play an important role in determining the assignment-compatibility. For instance, the type CheckingAccount is assignment-compatible with Account as it can be assigned to Account variable without any problem. When elements are assignment-compatible, their arrays are too. For instance, you can also assign CheckingAccount[] reference to an Account[] reference. Java automatically converts the type of each element from CheckingAccount to Account. Following code will compile.
CheckingAccount[] checkingAccounts = new CheckingAccount[5];
Account[] acountList = checkingAccounts; // Valid
Account is a superclass of CheckingAccount, therefore checkingAccounts can be assigned to variable of type Account[]. Since CheckingAccount is narrower than interface-types Auditable and Reviewable, you can also assign the CheckingAccount[] to Auditable[] and Reviewable[] as:
Auditable[] auditables = checkingAccounts; // valid
Reviewable[] reviewables = checkingAccounts; // valid
Note that the interface types Auditable and Reviewable are wider than CheckingAccount. So far we discussed the rules of reference conversion in the context of assignment. Reference conversion also occurs when references are passed as arguments to a method.
|
|
Though the array reference conversions are discussed separately, they work same as the object reference conversions. You can convert any array reference into Object because it is the superclass and you can convert any array reference into Cloneable and Serializable because array class implements it. |