How Java identifies whether a location contains a primitive or link

If there is an object in the heap and this object has several instance variables, some of them are primitive types, and some others. So, if this object has 5 fields, how is the object structured in memory? To be specific ... where does Java store the data type for each field? Are there some “flag bytes” and some “data bytes” where the “flag bytes” identify the data type of the next few “data bytes”?

I am referring to some additional details that go beyond the scope of this answer: https://stackoverflow.com/a/166778/

This answer contains more detailed information on how the data is stored in memory: https://stackoverflow.com/a/4148/

But he still doesn't say where the flag is stored, which says the data type is int / long / double / float / reference.

+4
source share
2 answers

Here is a more specific answer, which, I'm afraid, still does not answer your whole question. Here is the link from java 7 docs, with the corresponding section: "2.11. Summary of the instruction set": https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-2.html

I will copy and paste some of them:

2.11.1. Types and Java Virtual Machine

Java , . , iload (§iload) , int, . Fload (§fload) float. , .

: int, l long, s , b , c char, f float, d double .

2.11.2.

(§ 2.6.1) (§ 2.6.6) Java (§2.6).

(§2.11.5) .

. .

+1

-. (, ) . , . C-.

, - , .

public static void main (String [] args) {
    int i = 0;
    int j = i + 1;
}

-:

public static void main(java.lang.String[]);
  Code:
     0: iconst_0
     1: istore_1
     2: iload_1
     3: iconst_1
     4: iadd
     5: istore_2
     6: return

, istore iload, iadd (i ).

, :

public static void main (String [] args) {
    Integer i = new Integer(0);
    int j = i + 1;
}

-:

public static void main(java.lang.String[]);
  Code:
     0: new           #2                  // class java/lang/Integer
     3: dup
     4: iconst_0
     5: invokespecial #3                  // Method java/lang/Integer."<init>":(I)V
     8: astore_1
     9: aload_1
    10: invokevirtual #4                  // Method java/lang/Integer.intValue:()I
    13: iconst_1
    14: iadd
    15: istore_2
    16: return

intValue() Integer , iadd.

, ( , istore " " ), . jrahhali.

+1

Source: https://habr.com/ru/post/1649947/


All Articles