• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Arrays belong to which class

 
Oceana Wickramasinghe
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arrays are Objects too. So which class do Arrays belong to, and does that class extend Object class?


The above program gives me the following output


[I
java.lang.Object
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Someone will probably explain it better than me, but, as your program shows the type of an array that contains ints is [I. An array of a different primitive type will be of a different type. And the type of an array of objects will be [L<class name>; For example, an array of Strings will be of type [Ljava.lang.String;
Try a few different array types in your program to see what I mean.

And yes, all the array types extend Object

Edit: Don't forget to try arrays of arrays e.g.
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:Someone will probably explain it better than me,...


I think that's pretty much it. I dont think there anything more which can be said.
 
Oceana Wickramasinghe
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okey but exactly which class is it? I searched all over the internet but couldnt find it. Doesnt java specification state where Arrays originate from. I know when you call getClass on an int array you get [I. What is this exactly?
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oceana Wickramasinghe wrote:Okey but exactly which class is it? I searched all over the internet but couldnt find it. Doesnt java specification state where Arrays originate from. I know when you call getClass on an int array you get [I. What is this exactly?

This is what Joanne stated in previous post:
[ - Is java's way of telling the JVM that the type for the variable is an array.
So when [L is used it means that the array is of some instances of some class and the class name follows [L
[I - Means an array of ints and int is not a class so it has a different representation.

Arrays is another class in Java, but that's used for a different purpose:
This class contains various methods for manipulating arrays (such as sorting and searching). This class also contains a static factory that allows arrays to be viewed as lists.


So arrays are themselves objects but they dont really represent any class unlike for example a List instance can be of ArrayList/Vector type.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oceana Wickramasinghe wrote:I know when you call getClass on an int array you get [I. What is this exactly?

[I is the name of the class. I would imagine the lack of information about array classes is due to the fact that they are an internal implementation feature of the JVM. By not publishing information about how they are implemented the implementation can be changed without causing any problems. All you need to know about an array is that it is a fixed size, it has a final variable called length which gives you its length and it can only contain data of the declared type.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic