• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

array as an object?

 
Minal Silimkar-Urankar
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I am writing statement
int[] a=new int[5];
This statement creates an array object. But there is no class 'Array' in java.lang. So how array is an object?
 
Ronald Schild
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think of an array as a special kind of object that is native to the java language. Also, declaration and initialization rules are different for an array.

You don't instantiate from an Array class, like:

Array myArray = new Array("int", 5) ; // Nonsense.

But more as:

int [] intArray = new int[14] ;

or:

int [] intArray = { 1, 2, 3, 4, 5, 1101 } ;

They are part of the language. And like any other object, array objects live on the heap. Array objects have all the methods that class Object has, plus the member 'length' that you can read to determine the length of the array (the number of components).

Edit: Some information from the JLS about arrays:

10.8 Class Objects for Arrays
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.
This is shown by the following example code:


which prints:

class [I
class java.lang.Object

where the string "[I" is the run-time type signature for the class object "array with component type int".

[ July 30, 2008: Message edited by: Ronald Schild ]
 
Vijitha Kumara
Bartender
Posts: 3916
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Isn't that mean there should be some kind of class representing the different types of arrays below the Object (i.e Object heirarchy)? I'm little confused .
[ July 30, 2008: Message edited by: Vijitha Kumara ]
 
Minal Silimkar-Urankar
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Ronald for detailed explaination.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vijitha Kumara:
Isn't that mean there should be some kind of class representing the different types of arrays below the Object (i.e Object heirarchy)? I'm little confused .

[ July 30, 2008: Message edited by: Vijitha Kumara ]


These classes exist. It's just that Java code provides a specific syntax for creating them that is different to the normal Object o = new Object() type syntax.
Read the JLS for full details, especially Chapter 10
 
Vijitha Kumara
Bartender
Posts: 3916
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Joanne.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic