Is there any performance detriment to assigning variables vs using them inline. I'm guessing this WOULD be worse if a method was returning primitive and I was 'boxing' it (e.g. method returning int, and me storing to Integer). What about something like the below?
String foo = buildFoo();
String bar = buildBar(foo);
return bar;
vs.
return buildBar(buildFoo());
For context as to why I'm asking (given some votes):
- I'm specifically asking this question because I am on a team and someone more senior than me told me specifically not to store values, but return them directly for optimization reasons.
- From what I've found online it seems if you box a primitive in a loop where you only need a primitive, it will still box and not optimize that out so it seems the optimizations done are not as robust as I would expect
- There is a-lot of confusion for me around java's compilation(s) and optimizations, and this only get compounded by multiple vendors and understanding if certain optimizations are java standards or vendor standards
I've studied compilers and optimizations at the VLSI level, but there's a difference to me in 'likely optimized out' and 'IS optimized out' especially when I'm getting feedback that it's not, and some things that seem trivial to optimize out seemingly aren't.