If we have a Type[], we can only store Type or its subtypes in it. The same goes for ArrayList. So why is it said that one is homogeneous while the other is not?
Arrays have a runtime check on the type of the added element. That is, if a new element that is not of the same type is added, an ArrayStoreException
is thrown at runtime. That's why they are considered as "homegeneous".
This is not true for ArrayList
s (List
s in general). Due to type erasure at runtime, it can practically hold any object.
The following throws an exception when running:
Object[] array = new String[3];
array[0] = "a";
array[1] = 1; // throws java.lang.ArrayStoreException
unlike the following which compiles and runs without problem (although with a compiler warning as it doesn't properly use generics):
ArrayList list = new ArrayList<String>();
list.add("a");
list.add(1); // OK
list.add(new Object()); // OK
With a correct use of generics, i.e. declaring the variable list
above of type ArrayList<String>
instead of ArrayList
, the problem is avoided at compile-time:
ArrayList<String> list = new ArrayList<String>();
list.add("a");
list.add(1); // compilation error
list.add(new Object()); // compilation error
But even with a generically declared list, you can have something like this work without an exception at runtime:
ArrayList<String> list = new ArrayList<String>();
list.add("a");
Method[] methods = List.class.getMethods();
for(Method m : methods) {
if(m.getName().equals("add")) {
m.invoke(list, 1);
break;
}
}
System.out.println(list.get(0));
System.out.println((Object) list.get(1));
Output:
a
1
Yes.Java
Arrays
are homogeneous,because when you declare any array in Java you have to declare its type
.
eg:
int arr[]; //type is int
String arr[]; //type is String
float arr[]; //type is float
now if you try to store any other data-type in declared array,it will be a compile time error. eg:
int arr=new int[5];
arr[0]="I am a String not int"; //compile time error
but ArrayList
are the Collection
's part,they hold Objects
,instead of any specific data-type
[if we are not talking about generics
],and because every thing in java is directly or indirectly inherited from Object
class
,so It will not give you compile-time error,
type checking will be on run-time
.
eg:
ArrayList al=new ArrayList();//type is Object
al.add("I am a String"); //Bacause String class in inherited from Object Class
al.add(1);//the int 1 will first autobox into Integer class then stored in al ArrayList.Now bacause Integer class is also inherited from Object class,it will*/ allow you to store
al.add(UserDefinedClass); //because every User defined class is also inherited from Object class,so it will also allow you.
Now did you notice,because we have not defined any data type of ArrayList al
,but still we are storing different type values: this is know why ArrayList
Stores Object
not specific data-type,thus they are heterogeneous not homogeneous.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With