2

please don't mind this is just a simple question on array length property. As a beginner in Java I came across Constants and final keyword, which is described as:

Constants are non-modifiable variables, declared with keyword final. Their values cannot be changed during program execution. Also, constants must be initialized during declaration. For examples:

final double PI = 3.1415926;  // Need to initialize

I have read nearly all the related posts, but I have a confusion about its initialization. I've tried to dive into its class using Netbeans IDE but it's implementation was not visible there.

What about the length field to get the length of an array?
http://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html#jls-10.7

How and when it is initialized (runtime/compiletime)?

Community
  • 1
  • 1
Vinit
  • 21
  • 4
  • constants are initialized in two different ways. The first way is when you declare it (shown by the pi example) or you could have it blank with say `final double PI;` then initialize it in your constructor. Those are the only ways to initialize a constant. – CoderMusgrove Jun 20 '14 at 21:04
  • @CoderMusgrove how about instance initializer blocks? – Bela Vizer Jun 20 '14 at 21:08
  • array are a language feature; length gets translated to arraylength opcode http://cs.au.dk/~mis/dOvs/jvmspec/ref--2.html – Lorenzo Boccaccia Jun 20 '14 at 21:15
  • 1
    @LorenzoBoccaccia So, the answer lies in opcode and its handling by JVM. – Vinit Jun 20 '14 at 21:46

2 Answers2

1

From Java Virtual Machine Specification. Chapter 3. Compiling for the Java Virtual Machine. 3.9 Arrays:

Java Virtual Machine arrays are also objects. Arrays are created and manipulated using a distinct set of instructions. The newarray instruction is used to create an array of a numeric type. The code:

   void createBuffer() {
        int buffer[];
        int bufsz = 100;
        int value = 12;
        buffer = new int[bufsz];
        buffer[10] = value;
        value = buffer[11];
    }

might be compiled to:

   Method void createBuffer()
    0   bipush 100     // Push int constant 100 (bufsz)
    2   istore_2       // Store bufsz in local variable 2
    3   bipush 12      // Push int constant 12 (value)
    5   istore_3       // Store value in local variable 3
    6   iload_2        // Push bufsz...
    //line below is what you're looking for [comment is mine]
    7   newarray int   // ...and create new int array of that length 
    9   astore_1       // Store new array in buffer
    10  aload_1        // Push buffer
    11  bipush 10      // Push int constant 10
    13  iload_3        // Push value
    14  iastore        // Store value at buffer[10]
    15  aload_1        // Push buffer
    16  bipush 11      // Push int constant 11
    18  iaload         // Push value at buffer[11]...
    19  istore_3       // ...and store it in value
    20  return

The newarray int instruction initializes the array and its length. Which means that the array length is initialized when you initialize the array, at runtime.

The explanation from link above also explains how an array of references is created by the anewarray instruction, and shows a similar pattern.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • Thanks @user3761592, So should i take your answer as .. array's length property is initialized at runtime at the just before its memory allocation, and that is done by newarray int . – Vinit Jun 20 '14 at 21:31
  • @Vinit I'm not user3761592. – Luiggi Mendoza Jun 20 '14 at 21:32
  • Sorry but i, just thanked him for his prompt answer. Also wanted to know if, i have truly understood the concept explained by you. Thanks @Luiggi Mendoza. – Vinit Jun 20 '14 at 21:40
1

That's a really good question actually!

This requires some in depth knowledge of how compilers work. I've spent time writing a C-flavored language compiler.

How it works is that constants are type-checked by the front end of the compiler during compile time. This ensures that they are the expected type and within the expected range given the other properties of the identifier being declared.

Now, most compilers have multi-token look aheads associated with them (all but the early languages have this feature). How this works is that during compile time, the statement within the '[' and ']' brackets undergoes syntactic and symtanic analysis by the front end of the compiler to ensure that it is valid and is known during compile time.

The length field of the array lies in the class/method area during the java runtime environment, so it would not be initialized until runtime.

  • Note that this is not stated in the Java Language Specification but in Java Virtual Machine Specification, as pointed in my answer. – Luiggi Mendoza Jun 20 '14 at 21:14