Dark Horse programmer--reflection

Source: Internet
Author: User


Reflection is the mapping of the various components in a Java class into corresponding classes
1.Class class: Java programs in the class belong to the same thing, you can use a class to describe such things, the name of this class is class
Each class object represents a single byte code in memory
Class does not have constructors
2. How to get the bytecode instance object
Class name.
Object. GetClass ()
Class.forName ("Class name"): Returns the bytecode, if it has already been loaded, returns directly if it does not exist, uses the ClassLoader to load, caches the loaded bytecode into the virtual machine

Nine predefined class instance objects: 8 basic data types and void

3.Constructor class
Get all construction methods for a class: GetConstructor ()
Get a constructor method: GetConstructor (parameter type)
Create instance object, Example: String str= (String) constructor.newinstance (parameter)

The *class object also has a newinstance () method: Creates an instance of the class represented by the class object (the constructor that calls the empty argument list)

Class 4.Field: A variable that represents a byte code, not a variable of an object
The GetField (String name) method of the class object can only get the visible member variable
Getdeclaredfield (String name): Gets all the member variables, but at this point only knows that there is a member in the class,
is still not available, if you want to use a private member, you need to call the Setaccessible (True) method, which is the violent reflex

5.Method class
To get a method in a class:
Method Chatat=class.forname ("Java.lang.String"). GetMethod ("CharAt", Int.class)//Two parameters are methods name and parameter respectively
Call method: If the first argument is null, the method object is considered a static
Charat.invoke (str,1) equivalent to Str.charat (1)
6. Reflection of the array:

An array of the same dimension and element type belongs to the same type, that is,
Have the same class instance object
An array of basic data types can be used as object types and cannot be used as object[] types;
A non-primitive type can be used as an object type or as a object[] type.
Arrays.aslist[] method to handle int[] and string[] time difference
The array tool class is used to perform reflection operations on arrays
7. In general, the two power objects of a class are equal when compared with the Equals () method, and their hash code is equal, and vice versa.
When an object is stored in the hashset, it is not possible to modify the object which participates in the calculation of the Hashcode field, otherwise the object's modified hash value is stored in the original
The hash value of the HashSet collection is not painful in this case, even if the contains () method uses the object's current reference
Retrieving an object as a parameter hashset also returns the result of the object not found. This also results in the inability to delete the current object from the HashSet collection.
This results in a memory leak (memory leak refers to the condition that the object is not deleted when it has no effect, and it is always memory-intensive).

Dark Horse programmer--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.