Java Foundation Enhancement-reflection mechanism

Source: Internet
Author: User

Reflection Cornerstone-Class (byte code)
/* As long as the types that appear in the source program have their own class instance objects, for example: int,int[],void*/
How to get the instance object (class type) corresponding to each byte code
1. Class name.
2. Object. GetClass
3.class.forname ("Class name"), (1. First forget the memory to find, whether there is a byte code has, is to use, no words on the way of using the class loader, get bytecode)
/* These three ways to get the same byte code is the same * *
Nine pre-defined class instance objects (eight basic data types + void)
1.class.isprimitive to determine whether a predefined instance object
2.int.class = = Integer.type (Integer.type Gets the byte code of the wrapped type)
3.int.class! = Integer.class (This is two different types, bytecode is also different)

Reflection
/* Reflection is the mapping of various components in the Java class (member variables, methods, constructs, packages, etc.) to the corresponding Java classes. */
1. Get the constructor method for a class Constructor
Constructor Constructor = Class.forName ("java.lang.String"). GetConstructor (Stringbuffer.class)
The Class.newinstance () method invokes the default parameterless construction method to create an object (using the caching mechanism to save the instance object of the default constructor method)

2. Reflection Field for member variables
Field name = Class.forName ("Xxx.xxx.Person"). GetField ("name"); Name is not a variable on an object (person), but on a class (person), it is used to fetch the corresponding value on an object
Field.get (person);//You need to specify which object is the member variable

3. Reflection method for member methods
String str1 = "abc"
Mehod Methodcharat = String.class.getMethod ("CharAt", Int.class); Get the member method of a class
System.out.println (Methodcharat.invoke (str1,1)); /* Result: b*///Call which object's this method, pass what argument
If the invoke first argument is passed to null, a static method that invokes the class

/*
* People draw a circle on the blackboard, involving three objects, the circle needs the center and radius, but is private, the method of drawing round
* Assignment to people is inappropriate.
*
* The driver takes the brakes, the driver only gives instructions to the train, and the braking action needs the train to finish.
*
* Interview often test object-oriented design, such as people close, people just to push the door.
*
* This is the Expert mode: who owns the data (private), who is the expert, the method is assigned to whom
*/

4. Use the reflection method to execute the main method in a class
The parameter required by the main method is an array of strings, and when the reflection calls the main method, the invoke pass parameter cannot be given directly to a string array parameter, because the string array will be disassembled and changed into multiple parameters when compiling.
And the main method as long as a parameter, so error resolution: The passed string array is encapsulated into an object object or a object[] object

5. Reflection of arrays
An array of the same dimension and element type belongs to the same type, that is, with the same class instance object
1. All the arrays
INT[][]A1 = new Int[3][3];
object[] O1 = A1; /* correct, int[] belongs to object*/
2. Eight large basic data types do not inherit from object
int[] A2 = new Int[3];
object[] O2 = A2; /* wrong, int does not belong to object*/

Note: the Arrays.aslist () method, in JDK 1.4, because/* does not have a variable parameter */, the received parameter is object[], the parameter received in JDK 5.0 is a variable parameter
Therefore, when System.out.println (Arrays.aslist (string[])); Prints out the values of the elements in the string array because this is/*jdk1.4 to compile */,
When System.out.println (Arrays.aslist (int[])); Because/*int does not belong to object*/,/*jdk1.4 cannot compile but 5.0 to compile (parse only one parameter) */, the element cannot be printed at this time, but the object is printed

6.ArrayList (ordered repeatable) and hashset (unordered and non-repeatable)
When the data is stored in the ArrayList, an object is a space, even if the two data are equal to each other, in the same order as in the ArrayList
And HashSet, is a whole mess of space, if you save data, two data equals, the back of that data will not be stored in

Set sets want to ensure that elements are not duplicated, can two elements be repeated according to what should be judged? (/* First call the new element Hashcode method, get the physical location, if there is no element, the new element is put here, if there is, then equals*/)
This is the Object.Equals method. However, if each additional element is checked once, the number of elements that are added to the collection is much more numerous when the element is many.
Beginners can understand that the Hashcode method actually returns the physical address of the object store (which may not actually be the case).
When the collection is to add a new element,/* First calls the element's Hashcode method */, it can immediately navigate to the physical location where it should be placed.
If there is no element in this position, it can be stored directly in this position, no further comparisons are made, and if there is already an element in this position, the Equals method of calling it is compared with the new element, the same is not saved, and the other addresses are hashed.

Note: Memory leaks in Java, such as something that is not available, want to remove it from memory, free up memory,
When an object is stored in the HashSet collection, it is not possible to modify the fields in the object that participate in the hash value, otherwise, the/* object's modified hash value differs from the hash value originally stored in the HashSet collection */,
Prevents the current object from being deleted separately from the HashSet collection, resulting in a/* memory leak */

7. Realization of the function of the frame with the function of reflection
Framework to solve the core issues, I am in the framework, you this user may still be in primary school still do not write the program, I wrote the framework program how to call to your later class, (do not know what kind of writing)
Because there is no way to know the class name to be called directly when writing a program, in a program, you cannot directly new the instance object of a class, but use reflection to do it (also need to read the configuration file)

Java Foundation Enhancement-reflection mechanism

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.