Why Is Reflection Slower Than new? Java Object Creation Performance Explained

This article compares creating Java objects with the new operator versus using reflection, presents benchmark code showing a huge speed gap, explains why reflection incurs overhead, and outlines practical scenarios where each approach is appropriate.

macrozheng
macrozheng
macrozheng
Why Is Reflection Slower Than new? Java Object Creation Performance Explained

1. Basic Concepts

In Java, objects are typically created with the new operator, but they can also be instantiated via reflection.

When should you choose new and when should you use reflection? How do their performance characteristics differ?

// new way
ReflectDemo reflectDemo = new ReflectDemo();

// three ways to create via reflection
// (1) Class<ReflectDemo> reflectDemoClass = ReflectDemo.class;
// (2) Class<?> aClass = Class.forName("com.whale.springtransaction.transactiondemo.reflectdemo.ReflectDemo");
// (3) Class<? extends Class> aClass = reflectDemoClass.getClass();

2. Performance Comparison Between new and Reflection

// test code
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));
    }
}

Running the benchmark shows that creating 100,000,000 objects with new is dramatically faster than using reflection.

Why Is There Such a Large Difference?

1. Java code is compiled to bytecode and executed on the JVM. The new path benefits from JIT optimizations, while reflection involves dynamic resolution that prevents many of these optimizations.

2. Reflection incurs additional overhead such as method visibility checks, parameter validation, and the inability to inline reflective calls.

Key Reasons for the Overhead

Method#invoke wraps and unwraps parameters.

Visibility checks are required.

Parameter type verification is performed.

Reflective calls are hard to inline.

JIT cannot optimize reflective code.

3. When to Use Reflection vs new

Typical reflection use cases:

Spring uses reflection to instantiate beans and place them in the IoC container.

Loading JDBC drivers with Class.forName().

Reverse engineering tasks such as decompilation.

Inserting a String into a generic ArrayList<Integer> via reflection.

Differences between new and reflection:

Reflection can access private members using setAccessible(), while new cannot. new requires the class name at compile time; reflection can instantiate classes without knowing the type beforehand.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

macrozheng
Written by

macrozheng

Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.