本文介绍了newInstance()vs new的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

调用newInstance()是否会受到惩罚,或者下面的机制是否相同?
newInstance()对新关键字*有多少开销?

Is there a penalty for calling newInstance() or is it the same mechanism underneath?How much overhead if any does newInstance() have over the new keyword* ?

*:折扣newInstance()暗示使用反射这一事实。

*: Discounting the fact that newInstance() implies using reflection.

推荐答案

在现实世界的测试中,通过Constuctor.newInstance创建一个类的18129个实例,传递10个参数-vs-创建实例通过新程序在时间上没有产生可测量的差异。

In a real world test, creating 18129 instances of a class via "Constuctor.newInstance" passing in 10 arguments -vs- creating the instances via "new" the program took no measurable difference in time.

这不是任何微观基准。

这是在Windows 7 x86 beta上的JDK 1.6.0_12。

This is with JDK 1.6.0_12 on Windows 7 x86 beta.

鉴于Constructor.newInstance将与Class.forName.newInstance非常相似我会说使用newInstance而不是新功能可以获得的开销很少。

Given that Constructor.newInstance is going to be very simlilar to Class.forName.newInstance I would say that the overhead is hardly anything given the functionality that you can get using newInstance over new.

一如既往,你应该自己测试看看。

As always you should test it yourself to see.

这篇关于newInstance()vs new的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 01:09