简体   繁体   中英

What does getClass() in Java actually return?

I am a C++ programmer currently picking up Java. I'm having trouble with digesting the getClass() method in Java, and I would greatly appreciate any insights you may have to offer. From my understanding, C++'s dynamic_cast and Java's getClass() are loosely related in the sense that they are different means to the same end. In C++, I could use dynamic_cast to determine polymorphic types, whereas in Java, I could use getClass() to determine an object's most derived class type. With a dynamic_cast, I can get back a pointer; with getClass(), I get back a class, but what does that return type (class) actually represent? Is it a pointer to the class?

From my understanding, C++'s dynamic_cast and Java's getClass() are loosely related

C++ typeid operator would provide for better comparison. The major difference is that in C++ typeid requires a class to have at least one virtual function, while getClass in Java can be called on objects of any type without restrictions.

what does that return type (class) actually represent?

It represents a Class object suitable for doing reflection on the class - something that has no direct counterpart in C++. For example, you can determine all interfaces the class implements, the base class of the class, list all methods, list all fields, call methods by name, and so on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM