Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Arrays  RSS feed

 
Joseph Sweet
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,

arrays are Java objects


BUT OBJECTS OF WHAT CLASS???
 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Object of class "array of X" where X is some object class or primitive.

int[].class is a class of its own, as are String[].class, Object[].class and even int[][][][][].class.

If you look at java.lang.Class, you see the following methods:
- boolean isArray()
- Class<?> getComponentClass()

This component type is the part before the [], so Integer.TYPE (the class representation of int), String.class, Object.class and int[][][][].class.


Have you ever noticed that when printing some objects, you get the type followed by an @ and a weird number? When you print arrays, you get something similar: first an L (indicating array), then the component type, then the @ and the weird number. That's because arrays have their own classes.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arrays are described in Chapter 10 of the Java Language Specification (JLS). Following are some highlights regarding arrays as objects.

Chapter 10...
In the Java programming language arrays are objects... All methods of class Object may be invoked on an array.


Section 10.7...
The members of an array type are all of the following:
  • The public final field length, which contains the number of components of the array (length may be positive or zero).
  • The public method clone, which overrides the method of the same name in class Object and throws no checked exceptions. The return type of the clone method of an array type T[] is T[].
  • All the members inherited from class Object; the only method of Object that is not inherited is its clone method.

  • Section 10.8...
    Every array has an associated Class object, shared with all other arrays with the same component type. The direct superclass of an array type is Object. Every array type implements the interfaces Cloneable and java.io.Serializable.

    [ October 01, 2007: Message edited by: marc weber ]
     
    Joseph Sweet
    Ranch Hand
    Posts: 327
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you guys.
    What does the weird number after the @ mean?
     
    marc weber
    Sheriff
    Posts: 11343
    Java Mac Safari
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Joseph Sweet:
    ... What does the weird number after the @ mean?

    It's a hexadecimal representation of the object's hashCode. Arrays do not override the toString method, so they inherit the implementation from Object, which the API describes as...

    getClass().getName() + '@' + Integer.toHexString(hashCode())
     
    Joseph Sweet
    Ranch Hand
    Posts: 327
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Wow, thanks

    Off topic: How does one acquire all this knowledge?
    I am afraid my knowledge in Java is too eclectic.
    Furthermore, I would not say I retain all the facts or insights that I used to have at some point in the past. Even when it comes to more practical APIs: there are just too many of them, so you can't use all of them all the time.... and when you don't use them, you tend to forget them.
    Not to mention the Java core subtle facts like in the "Java in general" forums, that I don't really happen to use them most of the time. Although I do enjoy to learn them.
     
    Rob Spoor
    Sheriff
    Posts: 21092
    85
    Chrome Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Joseph Sweet:
    Off topic: How does one acquire all this knowledge?

    Experience, lad. You should learn to appreciate it.
     
    Joseph Sweet
    Ranch Hand
    Posts: 327
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I do
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!