Difference between. getClass () and. class,. getclass. class
I have been thinking about the difference between. class and. getClass (). After thinking for a long time, I thought a little bit. Then I searched the internet and found the following article to share with you.
Originally, reflection involving java ~~~~~
Java reflection Learning
Reflection can be understood as an operation to obtain object type information during runtime. Traditional programming methods require programmers to determine the type used during the compilation phase. However, with the help of reflection, programmers can dynamically obtain the information and write more portable code. Strictly speaking, reflection is not a feature of a programming language, because reflection can be implemented in any language. However, if the programming language itself supports reflection, the reflection implementation will be much more convenient.
1. Obtain the type class
We know that everything in Java is an Object. The objects we use generally inherit directly or indirectly from the Object class. The Object class contains a method named getClass. You can use this method to obtainInstance.TypeIt refers to a class of the type, because everything is an object and the type is no exception. in Java, the type class is used to represent a type. All types are Class instances. For example, you have the following code:
[Java]View plaincopyprint?
A a = new A (); if (. getClass () =. class) {System. out. println ("equal");} else {System. out. println ("unequal");} output equal;
We can see that object a is an instance of A, and object A is a class. The result returned by using A. getClass () in the if statement is exactly the type class of,In Java, a type class of a specific type can be used"Type. class"ObtainBecause a. getClass () is A class of the type, that is, a. class, the result of the above code execution is to print "equal ". Note that the type classes correspond one to one,The type class of the parent class and the type class of the subclass are different.Therefore, if A is A subclass of B, the following code will get the output of "unequal:
[Java]View plaincopyprint?
A a = new A (); if (. getClass () = B. class) {System. out. println ("equal");} else {System. out. println ("unequal");} outputs unequal;
Therefore, if you know an instance, you can use the "getClass ()" method of the instance to obtain the type class of the object. If you know a type, you can use ". class.
2. Obtain the type information.
After obtaining the type class, you can call some of the methods to obtain the type information. The main methods include:
GetName (): String: Get the full name of this type.
GetSuperClass (): Class: Obtain the direct parent Class of this type. If this type does not have a direct parent Class, null is returned.
GetInterfaces (): Class []: obtains all interfaces implemented by this type.
IsArray (): boolean: determines whether this type is an array.
IsEnum (): boolean: determines whether the type is an enumeration type.
IsInterface (): boolean: determines whether this type is an interface.
IsPrimitive (): boolean: determines whether the type is of the basic type, that is, whether it is int, boolean, double, and so on.
IsAssignableFrom (Classcls): boolean: determines whether this type is a cls parent class or parent interface.
GetComponentType (): Class: If this type is an array, the component type of this array is returned.
In addition, you can perform operations such as type conversion. The main methods are as follows:
AsSubclass (Class clazz): Class: set this type
Bytes -----------------------------------------------------------------------------------
I think of this question when learning reflection. Is there any difference between. getClass () and. class?
Of course, the most obvious difference is that. getClass () is the method of an object instance. Only the object instance has this method, and the specific class does not. ClassClass instanceIt is obtained through. class. Obviously, the class does not have the. getClass () method.
I also found some other materials on the Internet:
1. Different Periods of appearance: Class. forName () is loaded at runtime; Class. class and getClass () are loaded in the compiler, that is. class is static load ,. getClass () is dynamic loading.Are there any questions? Class. forName ("XXX") This method is to dynamically load the class, first load the Class file, and then use. newInstance () to create an object.
New ClassName () is called static loading,
Class. forName ("ClassName") is called dynamic loading.
The difference is that the "static loading" class must be provided during compilation, while the Dynamically Loaded class can be absent during source program compilation.
Class. forName (xxx. xx. xx) returns a class ,. newInstance () before creating an object Class. forName (xxx. xx. xx); the JVM is required to search for and load the specified class, that is, the JVM will execute the static code segment of the class.
Refer
Http://www.blogjava.net/William/archive/2006/08/25/65804.html
2. For example, Iterator it = s. iterator (); the real type of it obtained is KeyIterator, which is a subclass of Iterator. Generally speaking, the next () method can be executed. However, it is worth noting that, keyIterator is the internal class of hashmap. The prompt in JAVA is cannot access a member of class java. util. hashMap $ KeyIterator withmodifiers "public"
From the examples above, we can see that the. class function is completely equal to. getClass () in applications other than internal classes ()! Only one is obtained directly by class, and the other is obtained by instance.