In Java is there a performance difference between referencing a field through getter versus through a variable?

后端 未结 9 1310
失恋的感觉
失恋的感觉 2020-12-20 17:49

Is there any differences between doing

Field field = something.getSomethingElse().getField();
if (field == 0) {
//do something    
}
somelist.add(field);
         


        
9条回答
  •  猫巷女王i
    2020-12-20 18:41

    There is a difference in that accessing variables through getters results in a method call. The JVM might conceivably be able to optimize the method call away under some circumstances, but it is a method call.

    That said, if the biggest bottleneck or performance problem in your code is overhead from accessor methods, I would say that you don't have a lot to worry about.

提交回复
热议问题