If reflection in Java slows down execution by orders, why do so many frameworks use it ?

后端 未结 2 1418
暗喜
暗喜 2020-12-31 10:57

As per my understanding, use of Java reflection API slows down code execution by orders. But then I see it being used in so many places in Java universe. To name a few :

2条回答
  •  没有蜡笔的小新
    2020-12-31 11:39

    Main point: because they have no other choice.

    Java is not a dynamic language, so the only way these frameworks can provide their services is by reflection.

    Second, notice that most of the reflection work these framework do happens only once, during initialization, so the runtime performance is not affected.

    About the performance of reflection

    There is one distinction that I notice being mixed up all the time:

    1. reflective lookup of members;
    2. reflective member access (invocation/read/write).

    Number 1 is slow (this is the "orders" you mention); number 2 is the one that has received significant speed improvements and is now only a couple of times slower than native access.

提交回复
热议问题