A programmer's tale

Wednesday, May 17, 2006

Finding field of array:

Which class contains the length field of an array in java? Let me elaborate the thing:

My code is:

public class ArrayLenghtTest {

static int[] a = {1, 2, 3, 4};

public static void main(String[] args) {

System.out.println("Length of the arrary:" + a.length);

            System.out.println(ia.getClass());
         
         System.out.println(ia.getClass().getSuperclass());

}

}

Here I’m accessing a field “lenght” of “a”. Now what is “a”? Here if “a” is not a class, how can I get a field from it? Then what is “a”? If “a” is a class then it must be implemented and instantiated somewhere. If you go through Lava Language Specification http://java.sun.com/docs/books/jls/third_edition/html/j3TOC.html), you will find that there are primarily two kind of types in java: 1. primitive type and 2. reference type. In our case “a” is definitely not a primitive type , hence it’s a reference type. Again reference type is divided into three sub-types: a) class type, b) interface type and c) array types. Now we have got that “a” is a array type.

If we go little further in JLS3.0 http://java.sun.com/docs/books/jls/third_edition/html/arrays.html we can find that

“In the Java programming language arrays are objects (§4.3.1), are dynamically created, and may be assigned to variables of type Object (§4.3.2). All methods of class Object may be invoked on an array.

An array object contains a number of variables. The number of variables may be zero, in which case the array is said to be empty. The variables contained in an array have no names; instead they are referenced by array access expressions that use nonnegative integer index values. These variables are called the components of the array. If an array has n components, we say n is the length of the array; the components of the array are referenced using integer indices from 0 to n-1, inclusive.

All the components of an array have the same type, called the component type of the array. If the component type of an array is T, then the type of the array itself is written T[].

And for the members of array object we find here:

The members of an array type are all of the following:

  • The public final field length, which contains the number of components of the array (length may be positive or zero).
  • The public method clone, which overrides the method of the same name in class Object and throws no checked exceptions. The return type of the clone method of an array type T[] is T[].
  • All the members inherited from class Object; the only method of Object that is not inherited is its clone method.

But you cannot find the public final field length field anywhere in java SDK code.

The output of the above program is:
4
class [I
class java.lang.Object

Class [I is created by the native implementation of java.lang.reflect.Array; which is totally taken care by the JVM, the field length exists in that class i.e. [I. We cannot inatatiated or subclass that.

Here is a beautiful usecase for java array:

http://www.artima.com/legacy/answers/Aug2000/messages/149.html

0 Comments:

Post a Comment

<< Home