"Rob Griffiths" <r.***********@open.ac.uk> wrote in message
news:r.*********************************@europe.is p.giganews.com...
Can anyone explain to me the difference between an element type and a
component type?
In the java literature, arrays are said to have component types, whereas
collections from the Collections Framework are said to have an element
type.
http://java.sun.com/docs/books/jls/s...rrays.doc.html
states:
The *component type* of an array may itself be an array type. The
components of
such an array may contain references to subarrays. If, starting from any
array type, one considers its component type, and then (if that is also
an
array type) the component type of that type, and so on, eventually one
must
reach a component type that is not an array type; this is called the
*element
type* of the original array, and the components at this level of the data
structure are called the elements of the original array.
Reading that - I thought I'd 'got' the difference, but then in 10.1 (a
para later) the terms seem to used synonomously!
An array type is written as the name of an *element type* followed by
some
number of empty pairs of square brackets []. The number of bracket pairs
indicates the depth of array nesting. An array's length is not part of
its
type.
The element type of an array may be any type, whether primitive or
reference.
In particular:
€ Arrays with an interface type as the *component type* are allowed.
The
elements of such an array may have as their value a null reference or
instances of any type that implements the interface.
€ Arrays with an abstract class type as the *component type* are
allowed.
The elements of such an array may have as their value a null reference
or
instances of any subclass of the abstract class that is not itself
abstract.
When describing collections from the Collections framework there is
never any of this ambiguity/distinction - it is always "element type",
even when talking about a collection whose elements are themselves
collections.
Am I missing a "big idea"?
Any thoughts greatly appreciated.
Warning: I haven't read the latest JLS (I believe the one I read was for
Java 1.4), so I don't know if what changes there have been, but...
Arrays are treated specially in a lot of places in Java, and so some OO
purist recommend avoiding arrays altogether (and just using the ArrayList
class if you need array-like functionality). The syntax around arrays treat
them half like objects, half like primitive values. As far as I can recall,
only arrays have a "component type". So let me first start by definit
element type, and then I'll come back to "component type".
Collections are basically "a bunch of elements". The collection itself
has a type (e.g. "Vector", "ArrayList", "HashMap", etc.), and the elements
it contains also has a type. In 1.4 and prior, that type was always Object,
but starting from 1.5, using generics, you could narrow down the type of
elements. For example, ArrayList<Integer> has "Integer" as its element type.
ArrayList<TreeList<Comparable>> has TreeList<Comparable> as its element
type.
The parallel concept in array is "component type". The component type of
String[] is String. The component type of JTree[][][] is JTree[][]. The
*element* type of JTree[][][] is JTree. Confusing, yes, I know. They've
essentially swapped the meaning for "element type" between arrays and
collections.
Regarding this passage:
€ Arrays with an interface type as the *component type* are allowed.
The
elements of such an array may have as their value a null reference or
instances of any type that implements the interface.
Note that String is both the element type AND the component type of
String[]. So when they say "Arrays with an interface type as the component
type", that it equivalent (in all situations I could think of) of saying
"Arrays with an interface type as the element type".
However, there is a difference between "Arrays with an array as the
component type" and "Arrays with an array as the element type", with the
latter not making sense (the element type should never be an array).
I believe this strange naming system may be a "stuck" for backwards
compatibility reason. When you're using the reflections API, the class Array
has a method called getComponentType(), and they could not rename, or change
its behaviour, without breaking existing code.
- Oliver