Java GetClass (), Class () and iinstanceof difference and contact __java

Source: Internet
Author: User

GetClass () The classmate who has studied Python must be very familiar, is it a bit like __dict__ method? In fact, this introspection, called reflection in Java, can be understood to be almost identical to the operation of getting object type information at run time. Traditional programming methods require programmers to decide which types to use at compile time, but with the help of reflection, programmers can dynamically get the information to write more portable code. In a strictly speaking sense, reflection is not a feature of a programming language, because reflection can be implemented in any language, but if the programming language itself supports reflection, then the implementation of reflection is much more convenient. We know that everything in Java is an object, and the objects we normally use are inherited directly or indirectly from the object class. The object class contains a method named GetClass, which allows you to get the type class of an instance. A type class refers to a class that represents a type, because everything is an object, and the type is no exception, and the type class is used in Java to represent a type. All type classes are instances of class classes. For example, you have the following code:

A A = new A ();

if (A.getclass () ==a.class)
System.out.println ("equal");

else System.out.println ("unequal");
The result is to print out "equal". As you can see, object A is an instance of a, a class, and the result returned by using A.getclass () in the IF statement is the type class of a, which in Java represents a specific type of type class that can be obtained in the form of "type. Class" Because A.getclass () Get a type class, which is a.class, so the result of the above code is to print out "equal". It is particularly noted that the type class is one by one corresponding, the type class of the parent class is different from the subclass class, so if a is a subclass of B, the following code will get the output of "unequal":
A A = new A ();

if (A.getclass () ==b.class)
System.out.println ("equal");

else System.out.println ("unequal");

So, if you know an instance, then you can get the type class of the object through the "GetClass ()" Method of the instance, if you know a type, then you can use the ". Class" method to get the type class of that type.



in general, the GetClass () method and the class () method are equivalent, and you can get a type name, such as the following code:

When comparing a class to the same class instance as another class, we can usually use the instanceof and GetClass two methods to determine whether the two are equal, but the two are different in their judgment, and look at the differences in the code below:

public class Test {public static void testinstanceof (Object x) {System.out.pri  
        Ntln ("x instanceof Parent:" + (x instanceof parent));  
        System.out.println ("x instanceof Child:" + (x instanceof Child));  
        System.out.println ("x getclass Parent:" + (x.getclass () = = Parent.class));  
    System.out.println ("x getclass Child:" + (x.getclass () = = Child.class));  
        public static void Main (string[] args) {testinstanceof (New Parent ());  
        System.out.println ("---------------------------");  
    Testinstanceof (New Child ()); Class Parent {} class child extends Parent {}/* output: x instanceof parent:true x instanceof Ch 
Ild:false x getclass parent:true x getclass child:false---------------------------x instanceof parent:true x instanceof child:true x getclass parent:false x getclass child:true */

As you can see from the program output, the instanceof type checking rule is: Do you belong to the class? Or do you belong to a derived class of that class? It is a strict judgment to obtain the type information by getclass by using = = To check whether the equality is done. There will be no succession considerations;


The most direct difference between the two is that GetClass () is the method that an instance of a class has, and the class () method is a class method.
In addition, the getclass () is determined at run time, and the class () method is determined at compile time.

For example, the following program:

Class a{public
    void func () {
    }
}

class B extends a{

} public
class Test {public

    static void M Ain (string[] args) {
        A A = new A ();
        b b = new B ();
        A ab = new B ();
        System.out.println (A.getclass () + "" +a.class);
        System.out.println (B.getclass () + "" +b.class);
        System.out.println (Ab.getclass ());
        AB = A;
        System.out.println (Ab.getclass ());
    }

The output results are:

Class A Class A Class B Class B Class B class 
A

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.