Java reflection and new efficiency comparison

a basic concept

In Java, generally when we create an object, we may choose a new instance. But as our technology continues to improve, we have also learned that object creation can be achieved through reflection technology.

However, have you ever thought about it, when do we use new to create objects, and when do we use reflection to create objects?

What is the efficiency of creating objects between the two?

//new 方式创建对象
ReflectDemo reflectDemo = new ReflectDemo();
//反射创建对象  反射创建对象的三种方式
(1)Class<ReflectDemo> reflectDemoClass = ReflectDemo.class;
(2)Class<?> aClass = Class.forName ("com.whale.springtransaction.transactiondemo.reflectdemo.ReflectDemo");
(3)Class<? extends Class> aClass = reflectDemoClass.getClass ();

The efficiency comparison of two new objects and objects created by reflection

//测试代码如下
public class ReflectDemo {
    
    
	public static void main (String[] args) throws IllegalAccessException, InstantiationException {
    
    
		proxyObject();
		newObject();
	}

	//new 创建对象
	//5
	public static void newObject(){
    
    
		long startTime = System.currentTimeMillis ();
		int i;
		for (i = 0; i < 100000000; i++) {
    
    
			ReflectDemo reflectDemo = new ReflectDemo ();
		}
		if (i == 100000000) {
    
    
			long endTime = System.currentTimeMillis ();
			System.out.println ("new耗时为:" + (endTime - startTime));
		}
	}

	//反射 创建对象
	//30
	public static void proxyObject() throws IllegalAccessException, InstantiationException {
    
    
		long startTime = System.currentTimeMillis ();
		Class<ReflectDemo> reflectDemoClass = ReflectDemo.class;
		int i;
		for (i = 0; i < 100000000; i++) {
    
    
			ReflectDemo reflectDemo = reflectDemoClass.newInstance ();
		}
		if (i == 100000000) {
    
    
			long endTime = System.currentTimeMillis ();
			System.out.println ("反射耗时为:" + (endTime - startTime));
		}
	}
}

insert image description here

In the end, we found that the efficiency of new 100000000 objects and reflection to create 100000000 objects is many times different.

So let's discuss why there is such a big difference?

(1) First of all, generally our Java code needs to be compiled and run in the virtual machine.

First of all, we generally use a front-end editor, such as javac, to convert java files into class files.

Next, during the running of the program, a JIT, a just-in-time compiler, may be used to convert the bytecode file into a machine code file that the computer recognizes.

Another possibility is to directly compile java files into local machine code files through an AOT compiler. The JIT will optimize the program during the running of the program, but the reflection is through dynamic analysis, so it may not be able to perform some optimizations of the java virtual machine.

To sum up, there are several reasons:

1. The Method#invoke method will encapsulate and decapsulate the parameters

  1. Need to check method visible
  2. Need to verify parameters
  3. Reflection methods are hard to inline
  4. JIT cannot optimize

Three reflections and new usage scenarios

反射的部分使用场景 
  1.Spring通过反射来帮我们实例化对象,并放入到Ioc容器中
  2.使用JDBC链接数据库时加载数据库驱动Class.forName()
  3.逆向代码 例如反编译
  4.利用反射,在泛型为Integer的arryaList集合中存放一个String类型的对象
//new 对象和反射的区别
1.new的对象无法访问其中的私有属性,反射出来的可以通过设置setAccessible()方法来省略访问权限符。
2.new必须要知道类名,而反射创建对象不需要知道类型也可以创建

Guess you like

Origin blog.csdn.net/qq_43842093/article/details/131745760