logical structure/details of a reference variable and object in the memory?

后端 未结 4 1306
囚心锁ツ
囚心锁ツ 2021-02-09 11:47

Let\'s say we have a class:

class Class1
{
   int i = 1;
}

and we have a variable:

Class1 ob1 = new Class1();
4条回答
  •  被撕碎了的回忆
    2021-02-09 12:01

    Does a reference itself stored in a variable ob1 store the information that it refers to an object of Class1?

    NO. Reference variable ob1 stores only the reference of the object it points to. And the information about that object is already known to the application (or JVM).

    Does the part of the heap where Class1 is stored store the information that it is of Class1 type?

    NO. The information about class being loaded is stored in method area. As specified in this link
    For each type it loads, a Java virtual machine must store the following kinds of information in the method area:

    • The fully qualified name of the type
    • The fully qualified name of the type's direct superclass (unless the type is an interface or class java.lang.Object, neither of which have a superclass)
    • Whether or not the type is a class or an interface
    • The type's modifiers ( some subset of` public, abstract, final)
    • An ordered list of the fully qualified names of any direct superinterfaces

    How does logically looks like this information? It's a string like application1.Class1 or a reference to some reference types pool?

    Inside the Java class file and Java virtual machine, type names are always stored as fully qualified names. For example, the fully qualified name of class Object in package java.lang is representated as java/lang/Object. In the method area, fully qualified names can be represented in whatever form and data structures a designer chooses.

提交回复
热议问题