为什么Java Wrapper类没有非arg构造函数?

时间:2022-09-25 12:23:43

What is the rationale of not providing no-arg constructors in Wrapper Classes? I know that they were inherently built for Wrapping primitive types, so the right way is to provide a primitive type for constructors. But considering primitive types have no-arg constructor, why don't they have one?

在Wrapper类中不提供no-arg构造函数的理由是什么?我知道它们本身是为包装原始类型而构建的,所以正确的方法是为构造函数提供原始类型。但是考虑到原始类型没有arg构造函数,为什么它们没有?

Besides, if they had no-arg constructors, they could be instantiated as T.class.newInstance(). However, since newInstance() requires no-arg constructor, this won't work with Wrapper Classes.

此外,如果它们没有arg构造函数,则可以将它们实例化为T.class.newInstance()。但是,由于newInstance()需要no-arg构造函数,因此这不适用于Wrapper Classes。

Thanks in advance.

提前致谢。

Edit: Thanks John Topley for correcting my terminology.

编辑:感谢John Topley纠正我的术语。

6 个解决方案

#1


Wrapper objects are immutable. This means that once a wrapper object has a value assigned to it, that value cannot be changed. It doesn't make much sense to have a default value for an object whose value can't be changed. You wouldn't want to get a newInstance() of a wrapper class, because then you'd be stuck with the default value.

包装器对象是不可变的。这意味着一旦包装器对象具有分配给它的值,就不能更改该值。为无法更改其值的对象设置默认值没有多大意义。你不希望得到一个包装类的newInstance(),因为那样你就会陷入默认值。

#2


I think it's because the values wrapped by these classes are meant to be final immutable (that was the word I was looking for, thanks Bill:)). If there was a default constructor, it would be quite useless, as you couldn't change the the primitive wrapped by the class later on.

我认为这是因为这些类所包含的值是最终不可变的(这就是我要找的词,感谢Bill :))。如果有一个默认的构造函数,它将毫无用处,因为您无法在以后更改该类包装的基元。

#3


There's no use in providing the primitive type in a constructor. The type of the wrapper class indicates the primitive type. Since an instantiated wrapper object cannot change (immutable), there is only one chance of giving it a value: during its construction. If wrapper class objects were not immutable, strange things could happen. If you would have a default wrapper class constructor, what would its value be?

在构造函数中提供基本类型没有用处。包装类的类型表示基元类型。由于实例化的包装器对象不能更改(不可变),因此只有一次机会为其赋值:在构造期间。如果包装类对象不是不可变的,那么可能会发生奇怪的事情。如果你有一个默认的包装类构造函数,它的值是什么?

#4


A better question would be why do they have constructors at all. We should be just interested in the value. The object identity is irrelevant to the meaning of the types.

一个更好的问题是他们为什么要有构造函数。我们应该只对价值感兴趣。对象标识与类型的含义无关。

Most (but not all) uses of reflection are pointless. Construction of an immutable value like this would have very little value. Class.newInstance is particularly evil due to its exception behaviour. T.class where T is a generic parameter will not compile due to erasure.

大多数(但不是全部)使用反射都是毫无意义的。像这样建立一个不可变的值几乎没有价值。由于其异常行为,Class.newInstance特别邪恶。其中T是通用参数的T.class将因擦除而无法编译。

#5


Only objects have constructors, primitives don't have constructors so they don't have a default constructor. Primitives get their default value by virtue of objects/values being initialised to all 0 bytes. (Which is false in boolean, 0.0f in float, 0.0 in double and null as a reference)

只有对象具有构造函数,基元没有构造函数,因此它们没有默认构造函数。由于对象/值被初始化为全0字节,原语获得其默认值。 (布尔值为false,浮点数为0.0f,double为0.0,null为引用)

You appears to want to create an object with newInstance() however the only uninitialised value is null.

您似乎想要使用newInstance()创建一个对象,但是唯一未初始化的值为null。

#6


Most likely because while primitives have a default value ( 0, 0.0f, 0.0, 0L, false etc), the Wrappers usually express these default values as null.

最有可能的原因是,虽然基元具有默认值(0,0.0f,0.0,0L,false等),但Wrappers通常将这些默认值表示为null。

#1


Wrapper objects are immutable. This means that once a wrapper object has a value assigned to it, that value cannot be changed. It doesn't make much sense to have a default value for an object whose value can't be changed. You wouldn't want to get a newInstance() of a wrapper class, because then you'd be stuck with the default value.

包装器对象是不可变的。这意味着一旦包装器对象具有分配给它的值,就不能更改该值。为无法更改其值的对象设置默认值没有多大意义。你不希望得到一个包装类的newInstance(),因为那样你就会陷入默认值。

#2


I think it's because the values wrapped by these classes are meant to be final immutable (that was the word I was looking for, thanks Bill:)). If there was a default constructor, it would be quite useless, as you couldn't change the the primitive wrapped by the class later on.

我认为这是因为这些类所包含的值是最终不可变的(这就是我要找的词,感谢Bill :))。如果有一个默认的构造函数,它将毫无用处,因为您无法在以后更改该类包装的基元。

#3


There's no use in providing the primitive type in a constructor. The type of the wrapper class indicates the primitive type. Since an instantiated wrapper object cannot change (immutable), there is only one chance of giving it a value: during its construction. If wrapper class objects were not immutable, strange things could happen. If you would have a default wrapper class constructor, what would its value be?

在构造函数中提供基本类型没有用处。包装类的类型表示基元类型。由于实例化的包装器对象不能更改(不可变),因此只有一次机会为其赋值:在构造期间。如果包装类对象不是不可变的,那么可能会发生奇怪的事情。如果你有一个默认的包装类构造函数,它的值是什么?

#4


A better question would be why do they have constructors at all. We should be just interested in the value. The object identity is irrelevant to the meaning of the types.

一个更好的问题是他们为什么要有构造函数。我们应该只对价值感兴趣。对象标识与类型的含义无关。

Most (but not all) uses of reflection are pointless. Construction of an immutable value like this would have very little value. Class.newInstance is particularly evil due to its exception behaviour. T.class where T is a generic parameter will not compile due to erasure.

大多数(但不是全部)使用反射都是毫无意义的。像这样建立一个不可变的值几乎没有价值。由于其异常行为,Class.newInstance特别邪恶。其中T是通用参数的T.class将因擦除而无法编译。

#5


Only objects have constructors, primitives don't have constructors so they don't have a default constructor. Primitives get their default value by virtue of objects/values being initialised to all 0 bytes. (Which is false in boolean, 0.0f in float, 0.0 in double and null as a reference)

只有对象具有构造函数,基元没有构造函数,因此它们没有默认构造函数。由于对象/值被初始化为全0字节,原语获得其默认值。 (布尔值为false,浮点数为0.0f,double为0.0,null为引用)

You appears to want to create an object with newInstance() however the only uninitialised value is null.

您似乎想要使用newInstance()创建一个对象,但是唯一未初始化的值为null。

#6


Most likely because while primitives have a default value ( 0, 0.0f, 0.0, 0L, false etc), the Wrappers usually express these default values as null.

最有可能的原因是,虽然基元具有默认值(0,0.0f,0.0,0L,false等),但Wrappers通常将这些默认值表示为null。