Search code examples
javaperformancememory-efficientspace-efficiency

Does assigning same value to datatype decrease efficiency?


For example, I have an infinite while loop which keeps assigning true to our testBool:

boolean testBool;

while(true) {

    testBool = true;

    ...more logic...
} 

Are we wasting computation on reassigning the same value to our datatype, or are languages smart enough to recognize this inefficiency?


Solution

  • A smart compiler would pull it out of the loop.

    https://en.wikipedia.org/wiki/Loop-invariant_code_motion