Home » Java programming language

Disadvantages/ Limitations of Object Arrays in Java



In this article, we will learn about the disadvantages (limitations/drawbacks) of creating object arrays in java.
Submitted by Preeti Jain, on December 25, 2017

Disadvantages of Object Arrays

1) We should know the size in advance which may not possible every time

Example 1: Declare Object Array with size

class ObjectArrays {
	public static void main(String[] args){
		Object[] a = new Object[10];
		System.out.println(a[0]);
	}
}

Output (Description)

Here, Object Array with size 10 and we are printing first element of Object Arrays, so we will get null as output because Object[] is an Object and Objects contain null by default if we don’t give any value.

E:\javasource>java ObjectArrays
null

Example 2: Declare Object Array without size

class ObjectArrays {
	public static void main(String[] args){
		Object[] a = new Object[];
		System.out.println(a[0]);
	}
}

Output (Description)

Here, Object Array without size and we are printing first element of Object Arrays so we will get compile time error because in Object[] size is compulsory we cannot instantiate Object arrays without size.

E:\javasource>javac ObjectArrays.java
ObjectArrays.java:8: array dimension missing
                                Object[] a = new Object[];
                                                         ^
1	error

2) Object Arrays don’t provide support readymade methods for every requirement. (i.e. Programmer is responsible to write the logic for such kind of requirement).

Example 1: Declare Object Array with size

class ObjectArrays {
	public static void main(String[] args){
		Object[] a = new Object[10];
		System.out.println(a[0].add("A"));
	}
}

Output (Description)

Here, add() method will give error it does not provide readymade facility.

E:\javasource>javac ObjectArrays.java
ObjectArrays.java:9: cannot find symbol
symbol  : method add(java.lang.String)
location: class java.lang.Object
                                System.out.println(a[0].add("A"));
								^
1	error

3) Memory wise Object Arrays is not recommended to use.

It can hold homogenous and heterogeneous elements while other Arrays can hold only homogeneous elements only.

Example 1: Declare Object Array with size

class ObjectArrays {
	public static void main(String[] args){
		Object[] a = new Object[10];
		a[0]   = new Integer[10];
		a[1]   = new Boolean[10];
		System.out.println(a[0]);
		System.out.println(a[1]);
	}
}

Output (Description)

Here, Object Arrays can hold Different or Same type of Objects In this program Object Class toString() method will call and print output in this format [email protected]_hexadecimal code(i.e. Integer;@360be0).

E:\javasource>java ObjectArrays
[Ljava.lang.Integer;@360be0
[Ljava.lang.Boolean;@45a877





Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.



Comments and Discussions


We are using Google to publish ads on our website; Google has its own privacy policies. They may save log, cookies on your system. Google may also collect information of your system like IP address, region, city, country. For more details please go through the Google’s privacy policy.