0% found this document useful (0 votes)
6 views

Object Array

An array is a referenced data type that allows the storage of multiple variables of the same or different types in contiguous memory locations. Heterogeneous elements can be stored in an array by creating an array of Object, which can hold any primitive or non-primitive types. The document also discusses array casting, exceptions related to incompatible types, and provides examples of how to create and manipulate arrays in Java.

Uploaded by

jovisec598
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Object Array

An array is a referenced data type that allows the storage of multiple variables of the same or different types in contiguous memory locations. Heterogeneous elements can be stored in an array by creating an array of Object, which can hold any primitive or non-primitive types. The document also discusses array casting, exceptions related to incompatible types, and provides examples of how to create and manipulate arrays in Java.

Uploaded by

jovisec598
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

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.

-> Can we store different types/heterogeneous elements in an array?


Ans : Yes

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.

3) How we can store different types/heterogeneous type elements in array ?


Ans : We have to create an array of Object

4) Syntax to create array of object ?


Ans : Object[] obj = new Object[arraySize];

5) What kind of elements we can store in Object array?


Ans : Any primitive type as well as any non-primitive type.

6)Demonstrate a program by storing heterogeneous elements in an array.

7) Can we store objects in array ?


Ans : Yes we can

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.

Example :Student s1 = new Student("Amar",101);


Student s2 = new Student("Jeet",104);
Student s3 = new Student("Virat",108);

//Creating Student array with size 3 to store 3 student objects


Student[] students = new Student[3];
students[0] = s1;
students[1] = s2;
students[2] = s3;

Array Casting and Exception in array casting


---------------------------------------------
-> For all referenced types and arrays including primitive arrays java.lang.Object
is super class.
-> Thats why we can store all referenced types arrays including primitive arrays
in Object class reference.
-> For every array object, JVM internally creates a class with name "datatype[]".

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

1. int[] i1 = new int[5];


2. int[] i2 = new short[5];
3. Object[] obj = new short[5];
4. Object obj1 = new int[5];
5. Object obj2 = new Object[5];

6. Example e1 = new Example[5];


7. Object[] obj = new Example[5];
8. Object obj = new Example[5];
9. Example[] ea = new Sample[5];
10. Example[] ea = new Test[5];

-> 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));

}
}

It is not identified by compiler because it checks only type of referenced


variable. In this case it considered objArray[0], objArray[1],objArray[2] variables
are of type java.lang.Object.

You might also like