• 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
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

array as an object?

 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Bartender
Posts: 4109
72
Hibernate Fedora Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Ronald for detailed explaination.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4109
72
Hibernate Fedora Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Joanne.
 
I need a new interior decorator. This tiny ad just painted every room in my house purple.
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic