Do I need to worry about performance in the case below and save the result of the expensive call, or does the compiler recognise it can do the expensive call once?
String name;
if (object.expensiveCall() != null) {
name = object.expensiveCall().getName();
}
The compiler will not (in general) do the call just once as it may have side effects, or may be volatile in its result – it couldn’t possibly know when you wanted it to ignore a second call and when you wanted it to actually make the call again. You need to save the result: