Java 布尔包装类如何实例化?
在java中,我可以编写这样的代码
Boolean b = true ;
并且它会起作用。我现在有一个保存值“true”的对象。
这是如何运作的?为什么我不必通过构造函数传递值?像这样:
Boolean b = new Boolean( true ) ;
另外,我可以创建可以以类似方式实例化的自定义类吗?如果是的话那叫什么?
这样我就可以做这样的事情:
Foobar foobar = "Test" ;
从而拥有我自己的包装类。
谢谢
In java, I can write code like this
Boolean b = true ;
And it will work. I now have an object that holds the value "true".
How does that work? Why don't I have to pass the value through a constructor? Like so:
Boolean b = new Boolean( true ) ;
Also, can I make custom classes that I can instantiate in a similar way? If so what is that called?
So that I can do something like this:
Foobar foobar = "Test" ;
And thus have my own wrapper class.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
不,你不能做后者。
前者称为 自动装箱 并在Java v1.5 自动换行,原语位于其对应的包装器中。
使用泛型和/或集合时,可以清楚地看到自动装箱的好处:
来自文章: J2SE 5.0 简而言之
在“原始类型的自动装箱和自动拆箱”示例中,我们有:
之前(添加了自动装箱)
之后
正如您所看到的,代码更加清晰。
请记住文档中的最后一条注释:
No you can't do the latter.
The former is called autoboxing and was introduced in Java v1.5 to auto wrap, primitives in their wrapper counterpart.
The benefit from autoboxing could be clearly been seen when using generics and/or collections:
From the article: J2SE 5.0 in a Nutshell
In the "Autoboxing and Auto-Unboxing of Primitive Types" sample we have:
Before (autoboxing was added)
After
As you see, the code is clearer.
Just bear in mind the last note on the documentation:
这是一个编译器功能。编译器会自动生成装箱操作。它实际上要做的是生成
因为这样现有的(不可变的)实例 Boolean.TRUE 和 Boolean.FALSE 将被使用而不是创建一个新的。
It's a compiler-feature. The compiler will automatically generate the boxing-operation. What it'll actually do is to generate
Because this way the existing (immutable) instances Boolean.TRUE and Boolean.FALSE will be used instead of creating a new one.
这与创建 String 对象的方式相同:
实际上,这只是 Java 中的一项额外功能。我也不知道为什么你会想要创建自己的包装类,考虑到任何原始数据类型已经有一个预定义的包装器......
It's the same way that you can make String objects as such:
It's just a perk in Java, really. I'm not sure why you would want to make your own wrapper class, either, considering that any primitive data type already has a predefined wrapper...
此功能已添加到 Java 1.5 中,称为 Autoboxing。这种魔法仅适用于原始值和相应的包装器。
而且您无法在 Java 中自己完成此操作。如果您仍然想要它,请选择 Scala - 太棒了!也就是说,您可以使用名为隐式转换的功能。这是您的案例的小例子:
This feature was added to Java 1.5 and it's called Autoboxing. This kind of magic available only to primitive values and correspondent wrappers.
And you can't do it yourself in Java. If you still want it, than go for Scala - it's great! Namely you can use feature called implicit conversions. Here is small example for your case:
不,这只是编译器的魔法;它将这些视为特殊情况(称为自动装箱)。请参见例如 http://download.oracle.com/javase /1.5.0/docs/guide/language/autoboxing.html。
No, it's just compiler magic; it treats these as special cases (known as autoboxing). See e.g. http://download.oracle.com/javase/1.5.0/docs/guide/language/autoboxing.html.
它被称为自动装箱,基本上只是编译器为你做的事情。它仅在 Java 5 中添加,在此之前您必须使用
new Boolean( true )
或(更好)Boolean.TRUE
。不,您不能将它用于您自己的类,它仅适用于原始包装类。
It's called autoboxing and is basically just something the compiler does for you. It was added only in Java 5, before that you did have to use
new Boolean( true )
, or (better)Boolean.TRUE
.And no, you cannot have it for your own classes, it's only done for the primitive wrapper classes.