aspose file tools*
The moose likes Java in General and the fly likes Arrays belong to which class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Arrays belong to which class" Watch "Arrays belong to which class" New topic
Author

Arrays belong to which class

Oceana Wickramasinghe
Ranch Hand

Joined: Mar 02, 2011
Posts: 77
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

Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3169
    
  10
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.


Joanne
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3056
    
  33

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.


Mohamed Sanaulla | My Blog
Oceana Wickramasinghe
Ranch Hand

Joined: Mar 02, 2011
Posts: 77
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

Joined: Sep 08, 2007
Posts: 3056
    
  33

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

Joined: Aug 05, 2005
Posts: 3169
    
  10
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Arrays belong to which class
 
Similar Threads
What is the main goal of using .class field
Doubt in Overloading / polymorphism.
class name of java arrays
Problem: String != String
Array element initialization