java 最佳实践:使用什么,包装类还是原始数据类型?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13902281/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 14:22:36  来源:igfitidea点击:

Best Practices : What to use, wrapper classes or primitive data types?

java

提问by Amar

In Java we have primitive data types and bunch of wrapper classes for them. My question is that when to use what? I know that when we need to create Collections, we will need to use the wrapper classes, but other than that are there other specific cases where one should use wrapper classes?

在 Java 中,我们有原始数据类型和一堆包装类。我的问题是什么时候使用什么?我知道当我们需要创建集合时,我们将需要使用包装类,但除此之外还有其他特定情况下应该使用包装类吗?

Also, is it that one should always use the primitive data types unless absolutely necessary?

另外,除非绝对必要,否则是否应该始终使用原始数据类型?

For example if I am creating a Class having an integer and a boolean properties:

例如,如果我正在创建一个具有整数和布尔属性的类:

Class MyClass {
    ...
    private Integer x;
    private Boolean y;
    ...
}

OR

或者

Class MyClass {
    ...
    private int x;
    private boolean y;
    ...
}

Which of them should be used more often? And in what scenarios the other one should be used?

哪些应该更频繁地使用?在什么情况下应该使用另一个?

回答by JB Nizet

Use the primitive type unless you have no other choice. The fact that it's not nullable will prevent many bugs. And they're also faster.

除非别无选择,否则请使用原始类型。它不可为空的事实将防止许多错误。而且它们也更快。

Besides collections, wrapper types are typically used to represent a nullable value (for example, coming from a database nullable column).

除了集合,包装器类型通常用于表示可为空的值(例如,来自数据库可为空的列)。

回答by Sawan

this articletalks about wrapper classes, they said:

这篇文章讨论了包装类,他们说:

The wrapper classes in the Java API serve two primary purposes:

1- To provide a mechanism to “wrap” primitive values in an object so that the primitives can be included in activities reserved for objects, like as being added to Collections, or returned from a method with an object return value.

2- To provide an assortment of utility functions for primitives. Most of these functions are related to various conversions: converting primitives to and from String objects, and converting primitives and String objects to and from different bases (or radix), such as binary, octal, and hexadecimal.

Java API 中的包装类有两个主要目的:

1- 提供一种将原始值“包装”在对象中的机制,以便原始值可以包含在为对象保留的活动中,例如添加到集合中,或从具有对象返回值的方法返回。

2- 为原语提供各种实用功能。这些函数中的大部分都与各种转换有关:将原语与 String 对象相互转换,以及将原语和 String 对象与不同的基数(或基数)相互转换,例如二进制、八进制和十六进制。

回答by rabz100

The wrapper classes are immutable, and therefore have no setter methods.Every time you want to change its value a new object would have to be created. This would create unnecessary objects. The int primitive just represents the bits of the number. You are not creating any new object. Therefore I would suggest using the primitive when not using collections.

包装类是不可变的,因此没有 setter 方法。每次要更改其值时,都必须创建一个新对象。这会创建不必要的对象。int 原语仅表示数字的位。您没有创建任何新对象。因此,我建议在不使用集合时使用原语。

回答by Woot4Moo

The wrapper classes can be quite helpful when your system produces a level of uncertainty that must be accounted for. For example using Boolean in a packet capture program such as wireshark, would allow you to pass along a null value if you were unable to determine if a packet was modified from the sender. Specifically in the case when you are able to determine other packet manipulations.

当您的系统产生一定程度的不确定性并且必须加以考虑时,包装类会非常有用。例如,在诸如wireshark 之类的数据包捕获程序中使用布尔值,如果您无法确定数据包是否被发件人修改,则允许您传递一个空值。特别是在您能够确定其他数据包操作的情况下。

回答by Olivier Jacot-Descombes

When using primitive types in c# I prefer to use the c# alias in declarations and .NET name when accessing static members. It's just a personal preference. The both names are absolutely equivalent.

在 c# 中使用原始类型时,我更喜欢在访问静态成员时在声明和 .NET 名称中使用 c# 别名。这只是个人喜好。这两个名字是绝对等价的。

int x = Int32.Parse("123");