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

后端 未结 9 1308
失恋的感觉
失恋的感觉 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条回答
  •  悲哀的现实
    2020-12-20 18:29

    There is a performance penalty ( which may be so small it is negligible ) Yet, the JVM may inline this and all the calls to improve the performance.

    It would be better if you leave it the second way.

提交回复
热议问题