When I am using any collection of generic type with Object
class and I'm storing different objects in that collection in that situation.
Can I say that the collection contains heterogeneous objects or not?
Heterogeneous object is a volumetric object with interior structure where different physically-based attributes are defined, e.g. spatial different material compositions, micro-structures, colour, density, etc. [32], [37].
1. A homogenous mixture is defined as the mixture in which the composition formed is uniform. On the other hand, a heterogeneous mixture is defined as the mixture in which the composition formed is not uniform.
In Java, an array is homogeneous, i.e. all its cells contain elements of the same type. Thus, an array of integers contains only integers (int), an array of strings — only strings, and an array of instances of a Dog class that we've created will contain only Dog objects.
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.
I've heard this term only once when a heterogeneous object referred to a heterogeneous collection, a collection that can store objects of different types.
A heterogeneous collection could be an Object[]
array, or List<Object>
list. We rarely use them (their declarations are too general - "a collection of everything" indicates design issues), but it's clear that it can contain instances of different types (e.g. Integer
and String
).
"homo-" means same, "hetero-" means different.
In any case if a single Java array can only store one type, say, only numbers, or only strings then it is homogeneous.
If multiple types then heterogeneous. In the above case, since collection is of Object type and can hold any type
Let's take Javascript for understanding this better eg. arrays are heterogeneous, because I can make an array that holds [5, "hello", new Object()], and in Java that's not possible.
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