Where is array's length property defined?

前端 未结 7 809
悲哀的现实
悲哀的现实 2020-11-22 13:41

We can determine the length of an ArrayList using its public method size(), like

ArrayList arr = new ArrayL         


        
相关标签:
7条回答
  • 2020-11-22 14:38

    To answer it as it-is, where is this length property of array defined? In a special Object header.

    Easy to see via JOL

     int [] ints = new int[23];
     System.out.println(ClassLayout.parseInstance(ints).toPrintable());
    

    One of the lines from this output is going to be:

    OFFSET  SIZE      TYPE DESCRIPTION
    16       4        (object header)   17 00 00 00 (00010111 00000000 00000000 00000000) (23)
    

    Usually Objects have two headers (mark and klass), arrays have one more that always occupy 4 bytes in length, as size is an int.

    0 讨论(0)
提交回复
热议问题