Java 布尔包装类如何实例化?

发布于 2024-10-15 18:48:30 字数 352 浏览 2 评论 0原文

在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

蓝海似她心 2024-10-22 18:48:30

不,你不能做后者。

前者称为 自动装箱 并在Java v1.5 自动换行,原语位于其对应的包装器中。

使用泛型和/或集合时,可以清楚地看到自动装箱的好处:

来自文章: J2SE 5.0 简而言之

在“原始类型的自动装箱和自动拆箱”示例中,我们有:

之前(添加了自动装箱)

ArrayList<Integer> list = new ArrayList<Integer>();
list.add(0, new Integer(42)); 
int total = (list.get(0)).intValue();

之后

 ArrayList<Integer> list = new ArrayList<Integer>();
 list.add(0, 42);
 int total = list.get(0);

正如您所看到的,代码更加清晰。

请记住文档中的最后一条注释:

那么什么时候应该使用自动装箱和拆箱呢?仅当引用类型和基元之间存在“阻抗不匹配”时才使用它们,例如,当您必须将数值放入集合中时。对于科学计算或其他性能敏感的数字代码,不适合使用自动装箱和拆箱。 Integer 不能替代 int;自动装箱和拆箱模糊了原始类型和引用类型之间的区别,但并没有消除它。

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)

ArrayList<Integer> list = new ArrayList<Integer>();
list.add(0, new Integer(42)); 
int total = (list.get(0)).intValue();

After

 ArrayList<Integer> list = new ArrayList<Integer>();
 list.add(0, 42);
 int total = list.get(0);

As you see, the code is clearer.

Just bear in mind the last note on the documentation:

So when should you use autoboxing and unboxing? Use them only when there is an “impedance mismatch” between reference types and primitives, for example, when you have to put numerical values into a collection. It is not appropriate to use autoboxing and unboxing for scientific computing, or other performance-sensitive numerical code. An Integer is not a substitute for an int; autoboxing and unboxing blur the distinction between primitive types and reference types, but they do not eliminate it.

吃→可爱长大的 2024-10-22 18:48:30

这是如何工作的?

这是一个编译器功能。编译器会自动生成装箱操作。它实际上要做的是生成

Boolean.valueOf(true);

因为这样现有的(不可变的)实例 Boolean.TRUE 和 Boolean.FALSE 将被使用而不是创建一个新的。

How does that work?

It's a compiler-feature. The compiler will automatically generate the boxing-operation. What it'll actually do is to generate

Boolean.valueOf(true);

Because this way the existing (immutable) instances Boolean.TRUE and Boolean.FALSE will be used instead of creating a new one.

近箐 2024-10-22 18:48:30

这与创建 String 对象的方式相同:

String s = "foobar"

实际上,这只是 Java 中的一项额外功能。我也不知道为什么你会想要创建自己的包装类,考虑到任何原始数据类型已经有一个预定义的包装器......

It's the same way that you can make String objects as such:

String s = "foobar"

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...

二货你真萌 2024-10-22 18:48:30

此功能已添加到 Java 1.5 中,称为 Autoboxing。这种魔法仅适用于原始值和相应的包装器。

而且您无法在 Java 中自己完成此操作。如果您仍然想要它,请选择 Scala - 太棒了!也就是说,您可以使用名为隐式转换的功能。这是您的案例的小例子:

case class Foobar(value: String)
implicit def convertStringToFoobar(s: String) = Foobar(s)
val foobar: Foobar = "Test";

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:

case class Foobar(value: String)
implicit def convertStringToFoobar(s: String) = Foobar(s)
val foobar: Foobar = "Test";
So尛奶瓶 2024-10-22 18:48:30

不,这只是编译器的魔法;它将这些视为特殊情况(称为自动装箱)。请参见例如 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.

国际总奸 2024-10-22 18:48:30

它被称为自动装箱,基本上只是编译器为你做的事情。它仅在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文