(Java Daily Talk: Day 10 - Employment Interview Knowledge Clearance Strategy) Java Basics - Generics and Reflection in Java

        How do Java generics work? no explanation

        What does reflection mean in Java? What are the application scenarios?

The Java reflection mechanism is in the running state. For any class, you can know all the properties and methods of this class; for any object, you can call any of its methods and properties; this dynamically obtained information and dynamic call The function of the method of the object is called the reflection mechanism of the java language.

Each class has a Class object, which contains information related to the class. When compiling a new class, a .class file with the same name will be generated, and the content of this file will save the Class object. Class loading is equivalent to the loading of Class objects, and classes are dynamically loaded into the JVM when they are used for the first time. You can also use Class.forName("com.mysql.jdbc.Driver") to control the loading of classes, which returns a Class object.

Reflection can provide class information at runtime, and this class can be loaded when the class is loaded, even if the .class file of the class does not exist during compilation. Class and java.lang.reflect together provide support for reflection. The java.lang.reflect library class mainly includes the following three classes:

Field: You can use the get() and set() methods to read and modify the methods associated with the Field object

Method: You can use the invoke() method to call the method associated with the Method() object

constructor: new objects can be created with constructor

        Reflection pros and cons?

Advantages: Judgment of runtime type, class.forName() dynamically loads classes, improving code flexibility;

shortcoming:

Although reflection is very powerful, it cannot be abused. If a function can be completed without reflection, it is better not to use it. Several warnings for using reflection technology:

        1. Performance overhead: reflection involves dynamic type analysis, so the JVM cannot optimize these codes. Therefore, reflective operations are much less efficient than non-reflective operations. We should avoid using reflection in frequently executed code or in performance-critical programs.

        2. Security restrictions: the use of reflection technology requires that the program must run in an environment without security restrictions

        3. Internal exposure: Since reflection allows code to perform operations that are not allowed under normal circumstances (such as: accessing private properties and methods), using reflection may cause some unexpected side effects, which may lead to code dysfunction , and breaks portability. Reflective code breaks the abstraction, so when the platform sends changes, the behavior of the code may change accordingly.

        In reflection, the difference between Class.forName and ClassLoader?

Both class.forName() and ClassLoader in Java can be used to load classes

class.forName() In addition to loading the .class file of the class into the jvm, the former will also interpret the class and execute the static block in the class.

The classloader only does one thing, which is to load the .class file into the jvm, and will not execute the content in the static, only in the newInstance will it execute the static block.

The Class.forName(name,initialize,loader) function with parameters can also control whether to load static blocks. And only when the newInstance() method is called, the constructor is called to create an object of the class.

        Three ways to get reflection in Java?

1. Realize the reflection mechanism through the new object

2. Realize the reflection mechanism through the path

3. Realize the reflection mechanism through the class name

Guess you like

Origin blog.csdn.net/weixin_50249953/article/details/124105431