Java原语是不变的吗? [英] Are Java primitives immutable?

查看:93
本文介绍了Java原语是不变的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果方法具有局部变量i:

If a method has a local variable i:

int i = 10;

然后分配一个新值:

i = 11;

这会分配一个新的内存位置吗?还是只替换原始值?

Will this allocate a new memory location? Or just replace the original value?

这是否意味着基元是不可变的?

Does this mean that primitives are immutable?

推荐答案

这会分配一个新的内存位置吗?还是只替换原始值?

Will this allocate a new memory location? Or just replace the original value?

Java并不能真正保证变量将与内存位置相对应.例如,您的方法可能以i存储在寄存器—中的方式进行了优化.或者编译器可以看到您从未真正使用过它的值,或者它可以跟踪代码并直接使用适当的值.

Java does not really make any guarantees that variables will correspond to memory locations; for example, your method might be optimized in such a way that i is stored in a register — or might not even be stored at all, if the compiler can see that you never actually use its value, or if it can trace through the code and use the appropriate values directly.

但是放在一边. . .如果我们在这里将抽象化为局部变量表示调用堆栈上的某个内存位置,那么i = 11将仅修改该内存位置上的值.不需要使用新的内存位置,因为变量i是唯一引用旧位置的东西.

But setting that aside . . . if we take the abstraction here to be that a local variable denotes a memory location on the call stack, then i = 11 will simply modify the value at that memory location. It will not need to use a new memory location, because the variable i was the only thing referring to the old location.

这是否意味着基元是不可变的?

Does this mean that primitives are immutable?

是和否:是的,基元是不可变的,但这不是因为上述原因.

Yes and no: yes, primitives are immutable, but no, that's not because of the above.

当我们说某物是可变的时,我们的意思是它可以被突变:在仍然具有相同标识的情况下进行更改.例如,当您长出头发时,您正在变异自己:您仍然是您,但是您的属性之一是不同的.

When we say that something is mutable, we mean that it can be mutated: changed while still having the same identity. For example, when you grow out your hair, you are mutating yourself: you're still you, but one of your attributes is different.

对于基元,它们的所有属性完全由它们的标识决定; 1始终表示1,无论如何,1 + 1始终是2.您无法更改.

In the case of primitives, all of their attributes are fully determined by their identity; 1 always means 1, no matter what, and 1 + 1 is always 2. You can't change that.

如果给定的int变量具有值1,则可以将其更改为具有值2,但这完全是标识的更改:它不再具有以前的值.就像将me更改为指向别人而不是指向我一样:它实际上并没有更改 me ,而只是更改了me.

If a given int variable has the value 1, you can change it to have the value 2 instead, but that's a total change of identity: it no longer has the same value it had before. That's like changing me to point to someone else instead of to me: it doesn't actually change me, it just changes me.

当然,有了对象,您通常可以同时做这两项:

With objects, of course, you can often do both:

StringBuilder sb = new StringBuilder("foo");
sb.append("bar"); // mutate the object identified by sb
sb = new StringBuilder(); // change sb to identify a different object
sb = null; // change sb not to identify any object at all

通常,这两种方法都将被描述为更改sb",因为人们会同时使用"sb"来引用变量(包含引用)和它所引用的 object (当它引用一个对象时).只要您记得重要时的区别,这种松散就可以了.

In common parlance, both of these will be described as "changing sb", because people will use "sb" both to refer the variable (which contains a reference) and to the object that it refers to (when it refers to one). This sort of looseness is fine, as long as you remember the distinction when it matters.

这篇关于Java原语是不变的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆