Object Array
Object Array
---------------
1) Definition of an Array :
----------------------------
An array is a referenced data type used to create fixed number of multiple
variables of same type/homogeneous type as a group to store multiple values of
similar type in contiguous memory locations with single variable
name.
2) Definition of array :
-----------------------
An array is a referenced data type used to create fixed number of multiple
variables of same type/homogeneous type or different type/heterogeneous(by creating
array of Object) as a group to store multiple values of similar type and different
type in contiguous memory locations with single variable name.
8)If we can store objects in array then what should be array type?
Ans : Its depends on the object type, if we are creating objects to store in array
then that type of array must be
declared as same class type or super class type.
For example
the class for int type array is "int[]"
the class for Example type array is "Example[]"
the class for Object type array is "Object[]"
-> For all array objects created with referenced types the super class is
"Object[]", which is subclass of
"Object"
-> The array objects created with primitive types are not compatible with each
other and their super
class is "Object" not "Object[]".
Object
---------------------------------------------------
| | | |
Example Student |->byte[] Object[]
| |->short[] -------------
Sample |->int[] | |
|->long[] Example[] Student[]
|->float[] |
|->double[] Sample[]
|->char[]
|->boolean[]
-> Find out compile time errors in below list of array objects conversion
-> java.lang.ArrayStoreException
-----------------------------------
JVM throws this exception, if it identifies the incompatible object is passed to
store in array location. If
this problem is identified by compiler, it throws CE: "incompatible types".
import java.util.*;
class Test {
public static void main(String[] args) {
Object[] objArray = new String[3];
objArray[0] = "abc";
objArray[1] = 10;//ASE
objArray[2] = "CDE";
System.out.println(Arrays.toString(objArray));
}
}