Correct way of thinking about primitive assignment

前端 未结 3 492
后悔当初
后悔当初 2020-12-21 06:09

In this example,

int x = 5;
int y = x;
x = 4;

y will remain 5 because x is just being reassigned and it is not manipulating the object it u

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-21 06:33

    primitive variables actually hold the value (not the reference). So at any point of time you create a primitive variable, a memory block (of that primitive type) gets reserved for a value, no matter you have assigned a value for that or not.

    (in any low level language you can think of a variable as a register)

提交回复
热议问题