When we see class, do we suddenly think of the class we wrote in the program? This class is not a class, the class is actually called the Java class collectively, we can call it abstract class is not the name of a specific class.
1. Concept
Each Java class in a Java program belongs to the same thing, and the Java class name that describes such things is class. Perhaps this explanation is still not clear enough, and then more detailed:
The Java class is used to describe the commonality of a class of things, what the properties of such things, there is no property, as to what the value of this property is determined by the instance object of this class, different instance objects have different property values, Java programs in each Java class, whether they belong to the same class of things, Is it possible to use a class to describe this kind of thing? So the name of this class is class.
Worth noting: different from lowercase class. The class class describes the names of classes, the class's access properties, the package name that the class belongs to, the list of field names, the list of method names, and so on, which is the basis of reflection.
2. Essence Analysis
As a simple example to explain: the name of a person is human, then the Java class name is class.
If we instantiate a person, then its instantiation object is Zhang San or John Doe, and so on, class class represents a Java class, then its individual instance object is what?
The instance object of class is corresponding to the byte code of each class in memory, for example, the bytecode of the person class, the byte code of the date class, and so on. A class is loaded into memory by the ClassLoader, and the contents of this space are the byte code of the class, the bytecode of the unused classes are different, and all of their contents in memory are different. So what's the specific byte code?
3. Byte code? Define
When we use the person class in the source program, we first need to compile this class's binary code on the hard disk into class on the hard disk, that is, a bunch of binary code, you need to load this group of binary code into memory, it can be used to create a set of objects, First of all to load the class bytecode into memory, and then use this bytecode to read a single object, when the program used a lot of classes, then there should be a few bytes in memory, each byte code is a class instance object.
We know that for the analogy we've seen before, as person, when we use its object, we can use:
Person P1=newperson (), but class cannot be directly obtained directly from the new class, such as
Class cs1= byte Code, CS1 represents a byte code in memory, for example, we will correspond to the corresponding object, it is
classcs1=date.class;//byte code 1
classcs2=person.class;//byte Code 2
How do I get a byte code?
Typically we can get to bytecode in three ways:
first kind name. Class For example: System.class
For example: classcls1=date.class;//byte code; The Date used here is a class, and the string we use often is not the primitive basic data type, but the class, typically there are eight basic data types, namely, Boolean, Byte , char, short, int, long, float anddouble.
The second object. GetClass () For example: New Date (). GetClass ( )
In the example above, for example, person p1=new person (), then P1 is an instantiated object is also P1 byte code to get the specific object, so we go to get its bytecode, that is, P1.getclass ();
The Third Kind of class.forname ("class name") such as: Class. forname ("Java.util.Date");
There is also a static method in class that is forname (); The class name in parentheses must specify the complete class name, such as class. forname ("java.util.String"), which is also equivalent to the abstract class to find a specific class, that is, the byte code of the String class, if the class itself has been loaded into memory, then do not need to load again.
The function of forname is to return bytecode, and there are two ways to return it:
1, this byte code has been loaded, has been in the loader inside, then directly back.
2, the Java Virtual machine does not have this byte code, then use the Nega loader to load, the loaded in the bytecode cache in the virtual machine, in the future to get this byte code will not have to load.
the best of three Ways is Class.forName ();
The third benefit is the dynamic compilation rather than the static, the first two are in the compile time has been determined by the type, but the third can be configured at runtime, because it is a string, then we can define a variable to store, that is, in the configuration file run in the dynamic assignment to its value.
It is worth noting that all three of these methods are available in bytecode, and three ways to get bytecode are actually the same byte code in memory.
String str1 = "abc"; Class cls1 = Str1.getclass (); Class cls2 = String.class; Class CLS3 = Class.forName ("java.lang.String"); System.out.println (cls1 = = CLS2); System.out.println (cls1 = = CLS3);
The result of printing is true, indicating that three ways to get bytecode are actually the same byte code in memory.
Ps: To determine whether a type is a basic data type, usually we can judge such code; int.class.isPrimitive (); In addition, we can use: Int.class==integer.type to see if it is a basic data type.
The TYPE attribute also applies to other analogies such as Boolean.type,character.type,byte.type,short.type,integer.type,long.type,float.type,double.type,void.type.
4. Summary
Understanding class classes lays the groundwork for understanding the reflection mechanism, which is the prerequisite for learning reflection.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java Tour (1)-class class