A thorough analysis of the underlying _class class of reflection

Source: Internet
Author: User

First, the cornerstone of reflection--->class class
1. The Java class is used to describe the characteristics of a class of things, what the properties of such things, there is no attribute, the value of this property of the range is determined by the instance object of this class, different instance objects have different property values. Each Java class in the Java program, whether they belong to the same kind of thing, is it possible to use a class to describe such things? The name of this class is class, pay attention to the difference between the lowercase class keyword OH. What kind of information does the class describe? The name of the class, the Access property of the class, the package name to which the class belongs, a list of field names, a list of method names, and so on. To learn reflection, you must first understand class.

    • Each Java class in a Java program belongs to the same class, and the Java class namethat describes such things is class.
      • public class class
    • Contrast question: What kind of a person does a lot of people say? What does a multitude of Java classes say?
      • Man->person
      • Java class->class
    • Comparison question: The person class represents people, and its instance object is Zhang San, John Doe such a specific person, the class class represents the Java class, its various instance objects corresponding to what?
      • A byte code corresponding to each class in memory, for example, the byte code of the Person class, the byte code of the ArrayList class, and so on.
      • A class is loaded into memory by the class loader, occupies a piece of storage space, the content of this space is bytecode, different classes of bytecode is different, so they are in memory of the content is different, this one by each object to represent, these objects obviously have the same type, what is this type?
    • How to get the instance object (class type) corresponding to each byte code
      • Class name. class, for example system.class//to get bytecode instances by class name
      • Object. GetClass (), such as New Date (). GetClass ();//Get bytecode instances by instance
      • Class.forName ("class name"); Class.forName ("Java.util.Date");//Suitable for cases where class bytecode has not yet been loaded into memory
        • Java.util.Date is a string that, if it is not known beforehand which class will be loaded, passes a string that dynamically loads the string specified by the class byte code.

    • Eight basic types and void also correspond to the class bytecode objects that represent themselves.
      • Class Cls1=int.class;
      • Class Cls2=void.class;

    • Class instance object of array type
      • Class.isarray ()
 PackageCom.itcast.day1; Public classReflecttest { Public Static voidMain (string[] args)throwsException {//three ways to get the byte code of string class in memory respectivelyClass cls1=string.class;//01. Class name.String str= "123"; Class CLS2=str.getclass ();//02. Example. GetClass ()String Classurl= "Java.lang.String"; Class CLS3=class.forname (Classurl);//Class.forName (class name)//three ways to get the same byte code. System.out.println (cls1 = = CLS2);//trueSystem.out.println (cls1 = = CLS3);//trueSystem.out.println (Cls1.isprimitive ());//false, not a basic type of byte codeSystem.out.println (int.class. isprimitive ());//true byte code of basic typeSystem.out.println (int.class= = Integer.class);//false The base type and the wrapper type are not a copy of the bytecodeSystem.out.println (int.class= = Integer.type);//true Integer.type represents the basic type of byte code for the wrapperSystem.out.println (int[].class. isprimitive ());//false Array is also a type, but not a basic typeSystem.out.println (int[].class. IsArray ());//true whether the array            } }

A thorough analysis of the underlying _class class of reflection

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.