How to check if two classes are equal in JAVA?

        Whether two classes are equal depends on whether they are loaded by the same class loader. If they come from different class loaders, then even if the two classes come from the same class file, they are not equal.

        So how can we tell if two classes come from the same loader?

        From various books and blogs, it can be learned that there are 5 stages of class loading: load verification prepare parsing initialization

        The loading phase obtains the binary byte stream that defines this class through the fully qualified name of the class, and converts the static storage structure of the byte stream into the runtime data structure of the method area. Generate a Class object in the heap (the Class object in Java7 is placed in the PermGen Space ( method area), but the HotSpot Jvm of Java8 removes the PermGen Space and adds the class variable to the heap area, so I think that the Class object will be after Java8 Allocated in the heap area ), initialize the class variables of this Class class in the preparation phase...

        It can be seen that a Class object corresponds to a class, or a class loader corresponds to a Class object. Then to judge whether two classes are equal, we can simply pass whether their Class objects are the same object.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324933513&siteId=291194637