Java Object Creation: new vs Reflection – Performance Comparison and Usage Scenarios
This article explains the fundamental differences between creating objects with the new operator and using Java reflection, presents benchmark code showing a large performance gap, analyzes the reasons behind the slowdown, and outlines practical scenarios where reflection is appropriate.
Basic Concepts
In Java, objects are typically instantiated using the new keyword, but developers can also create instances via reflection, which allows dynamic class loading and instantiation.
The article asks when to prefer new over reflection and how their performance compares.
Efficiency Comparison of new and Reflection
The following benchmark creates 100,000,000 objects using both approaches and measures the elapsed time.
// new way
ReflectDemo reflectDemo = new ReflectDemo();
// reflection ways
Class
reflectDemoClass = ReflectDemo.class;
Class
aClass = Class.forName("com.example.ReflectDemo");
Class
aClass2 = reflectDemoClass.getClass(); public class ReflectDemo {
public static void main(String[] args) throws IllegalAccessException, InstantiationException {
newObject();
proxyObject();
}
// 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
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 Performance Gap
1. Method#invoke wraps and unwraps parameters, adding overhead.
2. Reflection requires accessibility checks and security validations.
3. Reflective calls cannot be inlined by the JIT compiler.
4. JIT optimizations are limited for reflective code.
Reflection and new Usage Scenarios
Reflection use cases:
1. Spring uses reflection to instantiate beans and register them in the IoC container.
2. Loading JDBC drivers with Class.forName().
3. Reverse engineering or decompilation.
4. Inserting a value of a different type into a generic collection via reflection. // Differences summary
1. new cannot access private members directly; reflection can bypass access checks with setAccessible(true).
2. new requires the class name at compile time; reflection can instantiate without knowing the class name at compile time.In conclusion, while reflection provides powerful dynamic capabilities, it incurs significant performance costs compared to direct instantiation, and should be used only when its flexibility is essential.
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.