aspose file tools*
The moose likes Beginning Java and the fly likes array as an object? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "array as an object?" Watch "array as an object?" New topic
Author

array as an object?

minal silimkar
Ranch Hand

Joined: Nov 25, 2007
Posts: 133
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?


Minal Silimkar
Ronald Schild
Ranch Hand

Joined: Jun 09, 2008
Posts: 117
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 ]

Java hobbyist.
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3838

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 ]

SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
minal silimkar
Ranch Hand

Joined: Nov 25, 2007
Posts: 133
Thank you Ronald for detailed explaination.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3673
    
  15
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


Joanne
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3838

Thanks Joanne.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: array as an object?