• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Array Class

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I declare an array, for example



'i' is the reference of which class?
 
Ranch Hand
Posts: 175
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An array type is not a class but every array has an associated Class object.

According to the JLS

Every array has an associated Class object, shared with all other arrays with the same component type.

Although an array type is not a class, the Class object of every array acts as if:

  • The direct superclass of every array type is Object.
  • Every array type implements the interfaces Cloneable and java.io.Serializable.


  •  
    Marshal
    Posts: 76854
    366
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    In that code i does not refer to a lass, but hides a pointer to the array object. Try this, however:-
     
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:In that code i does not refer to a lass...


    Wish it did though. I rather like the idea of an array of lasses.

    Going back to Joe's point: I sometimes wonder why the JLS has to complicate things. An array is an object, it has a class, and the class has a name, so in what sense is an array type (as opposed to "an array" or "[]") not a class?

    The class of int[] is "[I", has a canonical name of "int[]" and is identical to int[].class; and the class of String[] is "[L]java.lang.String", has a canonical name of "java.lang.String[]" and is identical to String[].class.

    They don't make all these semantic distinctions with, for example, Enum, which is also a bit of an "oddball", so I wonder why they do it with array.

    Hands up who would like to see an API page for "[]" (or "T[]") presented as a class?
    Then you could spend some time explaining why the array qualifier is a "pesudo" class.

    Winston
     
    Campbell Ritchie
    Marshal
    Posts: 76854
    366
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    It would appear that I wrote:In that code i does not refer to a lass...

    And there is a grammatical error. It should say do not does. Ruth is the lass and people here can call me, “her lad.” In this part of the country you can be a lad or a lass almost any age.
     
    Campbell Ritchie
    Marshal
    Posts: 76854
    366
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
     
    Joe Bishara
    Ranch Hand
    Posts: 175
    17
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Winston Gutkowski wrote:Hands up who would like to see an API page for "[]" (or "T[]") presented as a class?


    I think it might complicate things further and things are already complicated enough

    An abstraction is supposed to strip out irrelevant details and focus on relevant details.

    A programmer does not have to explicitly declare an array type.

    final class int[] {}

    This is somehow implicitly done by the system possibly because these details are not relevant to the programmer since the syntax for creating an instance is unusual.

    int[] anArray = new int[10]

    If these details were exposed, the programmer may have to deal with unnecessary complications.

    Similarly, a programmer does not have to explicitly declare an enum type

    public final class Directions extends java.lang.Enum {}

    This is implicitly done by the system possibly for the same reason i.e. the syntax for creating an instance is unusual

    enum Directions {NORTH, SOUTH, EAST, WEST}
     
    Winston Gutkowski
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Joe Bishara wrote:An abstraction is supposed to strip out irrelevant details and focus on relevant details.


    I agree with your points about explicit declaration, but there are certainly relevant details about ALL arrays that are missing because there is no API page:

  • It has a public final int member called 'length'.
  • It extends Object - and in some cases Object[] as well, but this could be clarified in the documentation (if it existed).
  • It implements Serializable and Cloneable - implying a clone() method, which is extremely useful to know about because it returns a correctly typed array.

  • Also: ALL arrays are implicitly members of java.lang, regardless of the component type, which means they don't need to be imported.

    AFAIK, it is the only construct in the language that you have to consult the JLS for to get any information about; and that, IMO, is poor.
    It looks like a class and quacks like a class, so show it to us as a class and explain the differences in the documentation - including all the stuff about "re-ifabiltlity".

    Winston
     
    Won't you please? Please won't you be my neighbor? - Fred Rogers. Tiny ad:
    the value of filler advertising in 2021
    https://coderanch.com/t/730886/filler-advertising
    reply
      Bookmark Topic Watch Topic
    • New Topic