mardi 21 janvier 2020

Assignment of primitive type with the same value

I was wondering what are the implications of assigning a primitive type with the same value it is already assigned to

For example:

Int x = 5;
x a= x;

I realized it would probably remove the second statement anyway in the compilation stage. But this example could accure in a more complicated program. So my question is what does this code compile to. Does the compiler do something fancy i dont know about?

The reason im asking is, I have an if statement i would prefer to do in a trinary operator. But only if the condition is true i would like to alter the variable. Something like this:

Int x = //something;
x = x == someVar? 5 : x;

Is this less efficient than a regular if?

Thank you.

(If you are interested in the reason i need a trinary operator ill add an edit if you ask)

Aucun commentaire:

Enregistrer un commentaire