Java – which one to choose between calling the function twice and storing the return value in a variable?

I have the following situation I occasionally encounter similar situations Which is more suitable for the following two options?

Option 1:

String result = ( getDetails(...) == null ) ? "" : getDetails(...);

Option 2:

String returnValue = getDetails(...);
String result = ( returnValue == null ) ? "" : returnValue;

Which is more preferred and / or better

Solution

Option - 2: better

Option 1: unless getdetails (...) is a getter method (a single line method that returns something), you should always avoid using additional method calls and this situation

If you delve into micro optimization, it usually leads to method calls

>Assign stack to method variable > skip instruction set

These are a few, a lot of expenses As many say, such performance optimization should be left to the compiler, and the code should be written more readable

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>