In-depth study of Java. Lang. Class class

Source: Internet
Author: User
Preface:Java's library is growing and contains countless classes and interfaces. But there are some very important classes and interfaces, which are the core part of the Java class library. Common examples include string, object, class, collection, classloader.... Familiarizing yourself with these classes is the basis for learning java well. These classes are generally not easy to understand and can be mastered only by in-depth research and practice. The following is a summary of my understanding and use of these classes. You are welcome to leave your valuable comments and post-reading comments after reading!

When a Java program is running, the system always identifies all objects during the Java runtime. This information records the class to which each object belongs. Generally, virtual machines use the correct method for running type information. The class used to save these types of information is the class. The class encapsulates the status of an object and an interface during runtime. When a class is loaded, class objects are automatically created.
Class does not have a public constructor. The class object is automatically constructed by the Java Virtual Machine during class loading and by calling the defineclass method in the class loader. Therefore, a class object cannot be explicitly declared.
Virtual machines manage a unique Class Object for each type. That is to say, each class (type) has a class object. When running a program, the Java Virtual Machine (JVM) first checks whether the class object corresponding to the class to be loaded has been loaded. If the file is not loaded, JVM searches for the. Class file based on the class name and loads its class object.
The basic java types (Boolean, byte, Char, short, Int, long, float, and double) and keywords void also correspond to a class object.
Each array is a class mapped to a Class Object. All arrays with the same element type and dimension share the class object.
Generally, a class object of a class is loaded into the memory, which is used to create all objects of this class.

1. How to get the class object? Three methods are available:
1. Call the getclass () method of the object class to obtain the Class Object. This is also the most common method for generating class objects. For example:
Myobject X;
Class C1 = x. getclass ();
2. Use the static forname () method in the class to obtain the class object corresponding to the string. For example:
Class C2 = Class. forname ("myobject"), and employee must be the interface or class name.
3. The third method for retrieving class objects is very simple. If T is a Java type, T. Class represents a matched class object. For example
Class cl1 = manager. Class;
Class Cl2 = int. Class;
Class l3 = double []. Class;
Note: class objects only describe the type, which is not necessarily a class or interface. For example, the Int. Class above is a class object. For historical reasons, the getname method of the array type returns a strange name.

Ii. Common Methods for class classes
1. getname ()
A Class Object describes the attributes of a specific class, the most common method in the class, getname, returns the Object Name (class, interface, array class, basic type, or void) represented by this class object in the form of string.

2. newinstance ()
Class also has a useful way to create an instance for the class, which is called newinstance (). For example:
X. getclass. newinstance (), creates a new instance of the same type as X. The newinstance () method calls the default constructor (No parameter constructor) to initialize the new object.

3. getclassloader ()
Returns the class loader of the class.

4. getcomponenttype ()
Returns the class of the Array Component type.

5. getsuperclass ()
Returns the class that represents the superclass of the object (class, interface, basic type, or void) represented by this class.

6. isarray ()
Determines whether the class object represents an array class.

Iii. Some Usage skills of class
1. forname and newinstance can be used together to create an object based on the class name stored in the string. For example
Object OBJ = Class. forname (s). newinstance ();

2. virtual machines manage a unique Class Object for each type. Therefore, you can use the = Operator to compare class objects. For example:
If (E. getclass () = employee. Class )...

4. the API documentation for Java. Lang. Class from Sun has a Chinese version of CHM on the Internet. For convenience, I copied the class API documentation from Sun's javadoc site.

Java. Lang
Class <t>
java.lang.Object  java.lang.Class<T>
All Implemented interfaces:
Serializable, Annotatedelement, Genericdeclaration, Type
public final class Class<T>
Extends Object
Implements Serializable, Genericdeclaration, Type, Annotatedelement
ClassAn instance of the class indicates the classes and interfaces in the running Java application. Enumeration is a type, and annotation is an interface. Each array is a class mapped to a Class Object. All arrays with the same element type and dimension share ClassObject. Basic Java type ( boolean, byte, char, short, int, long, floatAnd double) And keywords voidAlso ClassObject.

ClassThere is no public constructor. ClassObjects are loaded by the Java Virtual Machine and by calling the class loader. defineClassMethod automatically constructed.

The following example uses ClassObject To display the Class Name of the object:

     void printClassName(Object obj) {         System.out.println("The class of " + obj +                            " is " + obj.getClass().getName());     } 
You can also use the JLS Section 15.8.2) To obtain the name type (or void) ClassObject. For example:

     System.out.println("The name of class Foo is: "+Foo.class.getName());

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.