Java Method invocation vs using a variable

前端 未结 14 2424
北海茫月
北海茫月 2020-11-27 02:57

Recently I got into a discussion with my Team lead about using temp variables vs calling getter methods. I was of the opinion for a long time that, if I know that I was goin

14条回答
  •  渐次进展
    2020-11-27 03:21

    It depends. If you would like to make it clear that you use the same value again and again, I'd assign it to a temp variable. I'd do so if the call of the getter is somewhat lengthy, like myCustomObject.getASpecificValue().

    You will get much fewer errors in your code if it is readable. So this is the main point.

    The performance differences are very small or not existent.

提交回复
热议问题