Why Reflection Is Much Slower Than new in Java: Performance Test & Usage Guide
This article compares the performance of creating objects with the new operator versus Java reflection, explains why reflection incurs a large overhead, and outlines scenarios where each approach is appropriate, helping developers choose the right technique for their code.
Basic Concepts
In Java, objects are typically created using the new operator, but they can also be instantiated via reflection.
The article asks when to prefer new over reflection and how their efficiencies differ.
// new way
ReflectDemo reflectDemo = new ReflectDemo();
// reflection ways
Class<ReflectDemo> reflectDemoClass = ReflectDemo.class;
Class<?> aClass = Class.forName("com.example.ReflectDemo");
Class<? extends Class> aClass = reflectDemoClass.getClass();Efficiency Comparison
public class ReflectDemo {
public static void main(String[] args) throws IllegalAccessException, InstantiationException {
proxyObject();
newObject();
}
// new object creation
public static void newObject() {
long startTime = System.currentTimeMillis();
for (int i = 0; i < 100000000; i++) {
ReflectDemo reflectDemo = new ReflectDemo();
}
long endTime = System.currentTimeMillis();
System.out.println("new耗时为:" + (endTime - startTime));
}
// reflection object creation
public static void proxyObject() throws IllegalAccessException, InstantiationException {
long startTime = System.currentTimeMillis();
Class<ReflectDemo> reflectDemoClass = ReflectDemo.class;
for (int i = 0; i < 100000000; i++) {
ReflectDemo reflectDemo = reflectDemoClass.newInstance();
}
long endTime = System.currentTimeMillis();
System.out.println("反射耗时为:" + (endTime - startTime));
}
}The test shows that creating 100,000,000 objects with new is many times faster than using reflection.
Reasons for the large difference include:
Method#invoke performs argument boxing/unboxing.
Reflection must check method visibility.
Reflection requires parameter validation.
Reflective calls cannot be inlined.
JIT cannot optimize reflective code.
When to Use Reflection vs. new
Reflection use cases:
1. Spring uses reflection to instantiate beans and place them in the IoC container.
2. Loading JDBC drivers with Class.forName().
3. Reverse engineering / decompilation.
4. Storing a String object in a generic List<int> via reflection. // Differences between new and reflection
1. new cannot access private members directly; reflection can bypass access checks with setAccessible().
2. new requires the class name at compile time; reflection can create instances without knowing the type beforehand.Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Architect's Tech Stack
Java backend, microservices, distributed systems, containerized programming, and more.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
