标签云

微信群

扫码加入我们

WeChat QR Code

I was wondering whether it was more efficient to use get() excessively on an object or store the return of the get() in a variable and use that. For example, would it be more efficient to do this:someObject.setColor(otherObject.getColor().r, otherObject.getColor().g,otherObject.getColor().b);or to store the it in a variable like thisColor color = otherObject.getColor();someObject.setColor(color.r, color.g, color.b);


Regardless of what's most efficient, I'm pretty sure choosing the least efficient will never be the primary bottleneck in anything you code.

2019年04月22日05分48秒

I don't know about efficiency from a performance point of view, but from a readability (and typing) point of view, the second approach is better

2019年04月22日05分48秒

Can you explain why in option two it does not rely on optimizations by the java run time?

2019年04月22日05分48秒

someFolk the possible optimization in option 1 is that the call to otherObject.getColor() is done only once, and the returned object is stored in a temporary variable. The java runtime might do that for you, or you do it explicitly for yourself as in option 2.

2019年04月22日05分48秒