Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

Arrays - component type vs Element type

Thread view: 
Rob Griffiths - 22 Jan 2006 22:55 GMT
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/second_edition/html/arrays.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.

Rob

r.w.griffiths@open.ac.uk
Oliver Wong - 23 Jan 2006 15:55 GMT
> Can anyone explain to me the difference between an element type and a
> component type?
[quoted text clipped - 51 lines]
>
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.