Array Length in Java: An interesting insight

 

       Java programmers use .length with arrays like anything, mostly remaining unaware of what does .length mean. Is it a method or an object or anything else? This article explores .length in details with all its peculiarities and specialities.
In Java, array is not a class, there are classes as  java.lang.reflect.Array and  java.util.Arrays  but unfortunately, java designers have decided not to design any class for array that we readily understand. 
So if we write
int arr[] = new int[5];

Then obviously, arr as coded above is not an object of any class. As per Oracle documentation “An array is a container object that holds a fixed number of values of a single type”.  Here it should be noted that the phrase “container object” as said in Oracle documentation, does not represent instance of class. So as arr is not an object, .length is obviously not a method (it does not have any braces either!). 
The code below shows that indeed arrays in Java are not at all java classes

public class ArrayLengthExplorer {

public static void main(String[] args) {
int arr[] = new int[5];
Class c = arr.getClass();
try {
System.out.println("Class Name:" + c.getName());
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

The output of the code yields the following
Class Name:[I


Now what is that “[I” ? It stands for the run-time type signature of an array with component type int

Now it is indeed not trivial to think for a container object that represents an array. Indeed array is something “special” in java, because it has its own predefined bytecode instructions where there is a field called “arraylength”, so .length is a programmatic manifestation of that “arraylength” field of bytecode instruction.

Here it has to be noted that this field “arraylength” has nothing to do with any field of the class object of type java.lang.Class for that array, rather there exists no field called length or arraylength. You can check it through the following code snippet


float arr[] = new float[5];
Class c = arr.getClass();
try {
System.out.println(c.getField("length"));
} catch (NoSuchFieldException e)
{
e.printStackTrace();
} catch (SecurityException e)
{
e.printStackTrace();
}

The above code will give the output as :

java.lang.NoSuchFieldException: length
at java.lang.Class.getField(Unknown Source)
at com.stncodes.ArrayLengthExplorer.main(ArrayLengthExplorer.java:9)

So we can think that arrays are treated specially in java, where length is basically a predefined field of its corresponding predefined bytecode instruction.

But the confusion should creep up with the oracle documentation saying “The public final field length, which contains the number of components of the array.” It primarily seems that length is some real fields of some real class, but it should be realized from other angles. Length of an array is public because it has to be accessed from everywhere, and it behaves like an immutable entity. In java the content of the array can be altered but the length of the array is fixed. So the official oracle documentation has to be understood through its spirit not through its literary meaning.

Hence as a final conclusion, the length of an array that is .length is a field of the predefined bytecode instruction that gets directly compiled from the source without any class signature and that field is immutable in nature.


No comments:
Write comments

Popular Posts