How arrays work in Java byte code

If I use a regular class, such as List, Vector, or something else, I get a function size()that returns the length of the class in question, but if I use the class array or the default data type, I get a publication member length that returns the current length of the array.

int a[] = new int[3];
a.length; // used without ()

Vector<Integer> v = new Vector<Integer>();
v.length(); // used with ()

Why? I mean, an array is not a proper class, is it? Therefore, if it is not a class, it cannot have a varaible element. I cannot understand how this is handled in the background (ByteCode). I know that an array in memory is stored with a pointer to the first element of the array and with index (i) the memory pointer is shifted by ArrayPointer + i*(size of DataType).

Now you can say that the computer goes through all the elements of the array and counts all the elements, but how does the computer know where the array ends and where the next one begins? And where does the "varaible member" come from in the array, where is the size stored?

I mean, we often use arrays, but I know so little what happens behind Java code in ByteCode. Can you explain to me how this is possible?

+4
source share
5 answers

Arrays are objects in Java, but they do not correspond to real classes. In fact, the JVM implicitly creates array classes on the fly, but for performance reasons, they are not actual classes.

, Object , . , - -.

-, newarray, anewarray multinewarray , . , new.

*aload *astore.

, x.length . arraylength. , .

public void test(int size){
    int[] x = new int[size];
    String[] y = new String[size];
    System.out.println(x.length);
    System.out.println(y.length);
}

-

.method public test : (I)V
    .limit stack 2
    .limit locals 4
    iload_1
    newarray int
    astore_2
    iload_1
    anewarray java/lang/String
    astore_3
    getstatic java/lang/System out Ljava/io/PrintStream;
    aload_2
    arraylength
    invokevirtual java/io/PrintStream println (I)V
    getstatic java/lang/System out Ljava/io/PrintStream;
    aload_3
    arraylength
    invokevirtual java/io/PrintStream println (I)V
    return
.end method

length - , .

.method static public main : ([Ljava/lang/String;)V
    .limit stack 1
    .limit locals 1
    aload_0
    getfield [Ljava/lang/String; length I
    return
.end method

Exception in thread "main" java.lang.VerifyError: Expecting reference to class i
n class ArrayTest at constant pool index 30 in method ArrayTest.main([Ljava/lang
/String;)V
        at java.lang.Class.getDeclaredMethods0(Native Method)
        at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
        at java.lang.Class.getMethod0(Unknown Source)
        at java.lang.Class.getMethod(Unknown Source)
        at sun.launcher.LauncherHelper.getMainMethod(Unknown Source)
        at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
+12
+1

, , , . .

0

- , , . :

public Object test(){
  int[] test = new int[5];
  Object obj = test; 
  return obj;
}

, : , , ( ). - [] + [indexnumber] * [sizeofeachitem], , .

0

! , ( , ), , , . length - final, , .

, Vector, , length() . String: String , length() CharSequence; CharSequence , .

-1
source

All Articles