-3

As we know that arrays are objects, so what is the class behind creation of array objects.

When we write a statement, int a[]=new int[56];

Here int is a primitive, it cannot be the class used for defining array objects,

Object can be the superclass of array objects, but it can't be the immediate class as it doesn't contain length field. So please help me in understanding this fact.

Rishab Shinghal
  • 591
  • 3
  • 16
  • There is no length method. There is a length *field*, and it is effectively defined as a member in every array "class". – Andy Turner Dec 31 '17 at 13:47
  • Array don't implements or extends any class. Think about new int[56] as a reference (or pointer if you like) to memory location where will be stored 56 ints. Also, arrays have length property, and much more methods to manipulate them in Arrays... – zlakad Dec 31 '17 at 13:47
  • ... because that's how the language is defined. Just because there's an `int` as part of its type doesn't mean `a` is not an object. Related: [Why isn't there a java.lang.Array class? If a java array is an Object, shouldn't it extend Object?](https://stackoverflow.com/q/8546500) – Bernhard Barker Dec 31 '17 at 15:03
  • How String is an object? Or Boolean? – Abhijit Sarkar Feb 03 '21 at 07:39

3 Answers3

2

JLS §4.3.1 defines an object as:

[...] a class instance or an array.

"Object can be the superclass of array objects" - This is not quite correct. There is no explicit class for arrays and thus another class cannot extend an array. The element-type of an array is independent and can be either a primitive or an object.

As to the question how the length of an array works, the JLS defines it in §10.3:

[...]

An array creation expression specifies the element type, the number of levels of nested arrays, and the length of the array for at least one of the levels of nesting. The array's length is available as a final instance variable length.

[...]

Turing85
  • 18,217
  • 7
  • 33
  • 58
  • I think you didn't get my question, I am saying array variables are objects, so these objects belong to which class? Which class contains the length instance variable which we use for calculating the length of array ( arr.len) , where arr is an array variable – Rishab Shinghal Dec 31 '17 at 13:40
  • @RishabShinghal edited the answer. Does this answer your question? – Turing85 Dec 31 '17 at 13:43
  • can you tell me how arr.length works if we say arr is an object – Rishab Shinghal Dec 31 '17 at 13:44
1

Arrays are builtin with Java itself, you can't find a class for it like you would for example String.

Arrays are Objects in java, that's true.

When we write a statement, int a[]=new int[56];

Here int is a primitive, it cannot be the class used for defining array objects...

int does represent primitive type values, but the object(array) is not of int type it just contains elements of type int.

can you tell me how arr.length works if we say arr is an object

Arrays ARE Objects as we already stated, length is a FIELD/ATTRIBUTE that every instance of array contains, that field is final and is created once you instantiate array:

int[] arrayOfInts = new int[23];

As you can see above we specify 23 for the size of our int[] and that size will be assigned to length field when our array is created, and we already said it's final so it can't be reassigned, we can only create new array not resize the current one.

Hope I cleared some of your confusion.

whatamidoingwithmylife
  • 1,119
  • 1
  • 16
  • 35
0

To put it in not-too-technical terms, arrays are a special kind of object. It's not an explicitly named class defined somewhere in the standard library like Map or String. It's more like something built into Java itself. It has its own special declaration syntax, but it behaves like all other objects.

Basically, for every primitive type or class defined, Java implicitly defines a corresponding array class that extends Object directly. It provides a special syntax for creating and accessing the array, but other than that, it is a plain object, with fields and overridden methods from Object, though those methods aren't really implemented to do anything useful.

Leo Aso
  • 11,898
  • 3
  • 25
  • 46