java 所有原始包装类都是不可变对象吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6166348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Are all primitive wrapper classes immutable objects?
提问by Jothi
Are all primitive wrapper classes in Java immutable objects? String is immutable. What are the other immutable objects?
Java 中的所有原始包装类都是不可变对象吗?字符串是不可变的。其他不可变对象是什么?
回答by Jon Skeet
Any type which doesn't give you any means to change the data within it is immutable - it's as simple as that. Yes, all the primitive wrapper types are immutable1, as is String
. UUID, URLand URI
are other examples.
任何不能给你任何改变其中数据的方法的类型都是不可变的——就这么简单。是的,所有原始包装器类型都是不可变的1,原样String
。UUID、URL和URI
其他示例。
Although Calendar
and Date
in the built-in Java API are mutable, many of the types within Joda Timeare immutable - and to my mind, this is onereason why Joda Time is easier to work with. If an object is immutable, you can keep a reference to it somewhere else in your code and not have to worry about whether or not some other piece of code is going to make changes - it's easier to reasonabout your code.
虽然Calendar
并Date
在内置的Java API是可变的,很多中类型的乔达时间是不变的-而在我看来,这是一个原因,约达时间是更易于使用。如果一个对象是不可变的,您可以在代码的其他地方保留对它的引用,而不必担心其他代码段是否会进行更改 - 更容易推理您的代码。
1by which I mean java.lang.Integer
etc. As noted elsewhere, the Atomic*
classes are mutable, and indeed haveto be in order to serve their purpose. There's a difference in my mind between "the standard set of primitive wrapper classes" and "the set of classes which wrap primitive values".
1我的意思是java.lang.Integer
等。正如其他地方所指出的,这些Atomic*
类是可变的,并且确实必须是为了达到它们的目的。在我看来,“标准的原始包装类集”和“包装原始值的类集”是有区别的。
You can write your own mutable wrapper class very easily:
您可以非常轻松地编写自己的可变包装类:
public class MutableInteger
{
private int value;
public MutableInteger(int value)
{
this.value = value;
}
public int getValue()
{
return value;
}
public void setValue(int value)
{
this.value = value;
}
}
So as you can see, there's nothing inherentlyimmutable about wrapper classes - it's just that the standard ones were designedto be immutable, by virtue of not providing any way to change the wrapped value.
因此,正如您所看到的,包装类没有本质上不可变的东西——只是标准类被设计为不可变的,因为没有提供任何方式来改变包装的值。
Note that this allows for the same object to be used repeatedly when boxing, for common values:
请注意,这允许在装箱时重复使用相同的对象,用于公共值:
Integer x = 100;
Integer y = 100;
// x and y are actually guaranteed to refer to the same object
Integer a = 1000;
Integer b = 1000;
// a and b *could* refer to the same object, but probably won't
回答by Frédéric Hamidi
Before Java 5, all the primitive wrapper classeswere immutable.
在 Java 5 之前,所有原始包装类都是不可变的。
However, the atomic wrapper classesintroduced in Java 5 (AtomicInteger
, AtomicLong
, AtomicBoolean
and AtomicReference<V>
) are mutable.
然而,原子包装类Java 5中引入(AtomicInteger
,AtomicLong
,AtomicBoolean
和AtomicReference<V>
)是可变的。
回答by Patchikori Rajeswari
Yes, of course. Wrapper classes are immutable.
当然是。包装类是不可变的。
You can read Why wrapper classes are immutable in java?to understand the immutability of wrapper classes.
您可以阅读为什么包装类在 Java 中是不可变的?了解包装类的不变性。
回答by Peter Lawrey
One odd "wrapper" class is Void
which doesn't have any valid objects, immutable or otherwise. It can only be set to null.
一个奇怪的“包装器”类Void
没有任何有效的对象,不可变的或其他的。它只能设置为空。
One use for Void
is to mark generic return types with no value. (You can't use primtive types or void
)
一种用途Void
是标记没有值的通用返回类型。(您不能使用原始类型或void
)
e.g.
例如
Callable<Void> callable = new Callable<Void>() {
public Void call() {
// do something
return null;
}
};
Even though Date
is technically mutable, I would describe it as "immutable by convension". It is generally understood or assumed you wouldn't change a Date object but would replace it to change it like any other immutable object.
尽管Date
在技术上是可变的,但我会将其描述为“约定不变”。通常理解或假设您不会更改 Date 对象,但会替换它以像任何其他不可变对象一样更改它。