Could someone explain me the .getClass() method in java

这一生的挚爱 提交于 2019-12-05 21:07:53

You first need to know how == and != compare the two operands. The reason why == and != cannot be used to compare reference types is that they actually compare the memory addresses of the two reference type variables.

So if I have two strings:

String x = "Hello";
String y = x;

Since x and y share the same memory address after the second line is executed, x == y evaluates to true.

The same goes for the getClass() method. The getClass() method returns the class of the object as a Class<T> object. The question is, why this evaluates to true:

x.getClass() == y.getClass()

The answer is simple. Because x and y are both of type String. So calling getClass will return the same instance. This means the two returned the objects share the same memory address.

"But when I compare strings with the same characters with the == operator, it evaluates to false!" you shouted.

This is because the strings are located at different memory addresses. But the classes that getClass will return is always at the same memory address if the class that they represent is the same. This is due to the way ClassLoader works. But I'm not an expert that.

You just need to know that the objects returned by getClass is at the same memory address if the classes they represent are the same.

The comparators == and != compare equality in the way of identity. So how this works for primitives is obvious. However it can also be used to compare objects. However most often this does not work as expected. There are some exceptions:

Strings are stored as literals, therefore if you define two instances of String containing the same value, they use the same literal. Think of this like both instances pointing to the same memory location.

Enums are basically a collection of constants, therefore an enum value is either the same instance or it is not. It cannot be that the enum has the same value but is another instance.

The same is true for Class objects, which is what you get when calling getClass(). A Class object is created by the ClassLoader the first time it is the *.class file is loaded. On subsequent calls the same Class object is used. Therefore Class objects may be compared with == and !=. However beware that if A.class is loaded by two different ClassLoaders, the class objects that you get returned from them are not of the same instance.

getClass() returns an instance (object) of the Class class. Since each Java class has a single instance of the Class class, if two objects belong to the same class, getClass() for those two objects will return the same isntance and therefore you can use == for comparing them, since == when applied to reference types determines if the two references refer to the same instance.

The .getClass method will just return the class of the object. When you declare a new instance of an object, it will be referring to a class. There can only be one class per jvm but multiple object referring to it. So when you get the class of two objects, they might be referring to the same class!

Handox

polymorphism is one of the most important feature for java. for example:

//this shows one use of getclass method.

public class Main {

    public static void main(String[] args) {
        Main m = new Main();
        Animal animal = new Animal();
        Human human = new Human();
        m.dosomething(animal);//print animal
        m.dosomething(human);//print human
    }

    private void dosomething(Animal an){
        System.out.println(an.getClass().toString());
        }

    }

class Human extends Animal{

    private void dance(){
        }
}

class Animal{

    private void eat(){
        }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!