Please take notes for reflection in Java!

Source: Internet
Author: User

The JAVA reflection mechanism is in the running state. For any class, all attributes and methods of the class can be known.

Can call any method and attribute of an object. This dynamically obtained information and dynamic call

The function of object methods is called the reflection mechanism of java.

When we first came into contact with reflection, we may be thinking, this is rarely used in programming.

How can this problem be solved? Yes, there is no reflection for common programming, but sometimes we upload a class to me on the network,

We do not know this class in advance. This requires dynamic loading. This is required at this time. For example, we all know that

Spring struts

Hibernate ibatis frameworks have the foundation of reflection.

I also used reflection. When I learned Java recently, I wrote a note to record it.

First of all, Class. Here C is in upper case. This is a class named Class. This Class is also from Object

Class. An instance of this class represents a bytecode file in Java. Note that

If this class has bytecode, there is only one copy. Since the Class is a Class, we can instantiate it,

We naturally think of new Class (), but this is not in Java. However, each Java class provides a getClass method,

The returned value of this method is a Class instance. We can also use "class name. class" to get the bytecode of this object.

Note that the eight basic data types in Java also correspond to bytecode. We can also use int. class.

A similar method is used to obtain the bytecode of the basic data type. Another interesting thing is that we often use void

There is a bytecode.

Since all eight basic data types have corresponding bytecode, we know that in Java

Is the two bytecode the same? Is the answer true? It is a basic data type and an object.

Java claims to be completely object-oriented, but it still provides basic data types that are not objects,

But after Java1.4, I can regard it as the corresponding encapsulation class object, because Java provides

Automatic unpacking and packing. However, all objects in Ruby include basic data types.

Let's take an example to verify what we mentioned above:
 
Import java. lang. reflect. Constructor;
 
Public class ReflectTest
{
Public static void main (String [] args) throws Exception
{
// There is only one bytecode in the memory. We can use the same bytecode to create different objects.
String str = "abc ";
Class cla1 = str. getClass ();
Class cla2 = String. class;
Class cla3 = Class. forName ("java. lang. String ");
System. out. println (cla1 = cla2 );
System. out. println (cla2 = cla3 );

System. out. println (cla1.isPrimitive (); // determines whether the data type is basic.
System. out. println (int. class. isPrimitive ());
System. out. println (int. class = Integer. class );
System. out. println (int. class = Integer. TYPE); // TYPE indicates the bytecode of the corresponding basic TYPE.
System. out. println (int []. class. isPrimitive (); // The array is not of the original type.
System. out. println (int []. class. isArray (); // checks whether a bytecode is an array.
}
 
}
True
True
False
True
False
True
False
True

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.