a:
4;
4.0f;
"text";
b:
4.0/5.3;
1.0f*2.3f+3.5f;
"super" + "man";
Questions
1) Does this code take up memory?
2) Does the code in b take up more memory than the code in a?
3) Could anyone explain in detail what happens internally?
EDIT:
“super” + “man” is a bad example, it’s not correct in the first place. (thanks to Michael Krelin)
Secondly, putting strings in the example was a bad idea.
It depends on the compiler, the computer’s architecture, etc.
The compiler will most likely calculate the values of these expressions at compile time if it can be sure that the result is always the same. The result of the expression can then be placed directly into the output file, instead of the instructions needed to calculate the result. But as far as I am aware the compiler is not required to perform these sorts of optimizations.
If the expression is evaluated at compile time, the performance at runtime will be the same as if you wrote the result directly in the source code.