Java 原始类型:int 与 Integer

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

Java Primitive Types: int vs. Integer

javaandroidtypes

提问by tjb

I am confused about when to use primitive vs. non-primitive(?) types (i.e. int vs. Integer) in Java. I realize that in some places you can't use primitive types (for example when making use of generics). But what about in "normal" code? Is there a performance penalty for using non-primitive types? What about when working with Android?

我很困惑何时在 Java 中使用原始类型与非原始(?)类型(即 int 与 Integer)。我意识到在某些地方你不能使用原始类型(例如在使用 泛型时)。但是在“正常”代码中呢?使用非原始类型会有性能损失吗?使用 Android 时怎么样?

***My question is very similar to this question, which was discovered by one of the posters below. The answers to the linked question give additional insights into this question, which are not covered below.

***我的问题与这个问题非常相似,这是由以下海报之一发现的。链接问题的答案提供了有关此问题的更多见解,以下未涵盖。

*** "non-primitive" types are officially referred to as reference types.

*** “非原始”类型被正式称为引用类型。

回答by eggie5

Short answer: An intis a number; an Integeris a pointer that can reference an object that contains a number. Using Integerfor arithmetic involves more CPU cycles and consumes more memory. An intis not an object and cannot passed to any method that requires objects (just like what you said about Generics).

简答:Anint是一个数字;anInteger是可以引用包含数字的对象的指针。使用Integer算术涉及更多的CPU周期和占用更多的内存。Anint不是对象,不能传递给任何需要对象的方法(就像你说的泛型一样)。

回答by Heatsink

Non-primitive types are objects. They have to be dynamically allocated, garbage collected, and checked for null-ness (although some of these operations may get removed by an optimizing compiler). Reading their actual value requires loading from a pointer. Primitive types are values. They generally take up less space and are faster to access.

非原始类型是对象。它们必须动态分配、垃圾收集并检查是否为空(尽管其中一些操作可能会被优化编译器删除)。读取它们的实际值需要从指针加载。原始类型是值。它们通常占用的空间更少,访问速度更快。

A good rule of thumb is, use primitive types unlessyou need polymorphism, in which case use the corresponding object.

一个好的经验法则是,除非需要多态,否则使用原始类型,在这种情况下使用相应的对象。

回答by Hyman Edmonds

There is a slight penalty for converting between the types (autoboxing). Also intwill have a bit less overhead so I would always go with intif you can.

在类型之间转换(自动装箱)会有轻微的惩罚。也int将有少一点的开销,所以我将与经常去int,如果你能。

Also see this question: When to use primitive and when reference types in Java

另请参阅此问题:何时在 Java 中使用原始类型以及何时使用引用类型

回答by Arshak Manukyan

In Java, intis a primitive data type, while Integeris a Wrapper class.

在Java中,int是原始数据类型,Integer而是Wrapper类。

int, being a primitive data type has less flexibility. We can only store the binary value of an integer in it. Since Integeris a wrapper class for intdata type, it gives us more flexibility in storing, converting and manipulating integer data. Integeris a class and thus it can call various in-built methods defined in the class. Variables of type Integerstore references to Integerobjects, just as with any other reference (object) type.

int,作为原始数据类型的灵活性较低。我们只能在其中存储整数的二进制值。由于Integerint数据类型的包装类,它为我们在存储、转换和操作整数数据方面提供了更大的灵活性。 Integer是一个类,因此它可以调用class. 类型变量Integer存储对Integer对象的引用,就像任何其他引用(对象)类型一样。

You can find a more detailed explanation here.

您可以在此处找到更详细的说明。

回答by alphazero

As an OO purist, you would likely shun the primitives altogether and damn the performance costs and lack of postfix operators. (Yes, there is a performance cost.) You may also adopt this approach simply from extensibility considerations as a designer (without necessarily being hung up on purity.)

作为一个 OO 纯粹主义者,您可能会完全避开原语,并且该死的性能成本和缺乏后缀运算符。(是的,有性能成本。)作为设计师,您也可以仅从可扩展性考虑中采用这种方法(不必拘泥于纯度。)

As a practical matter (outside of theoretical and aesthetic questions), use the primitives everywhere you can and use the object version where you can't use primitives. (You already mentioned one such case. The language and APIs will drive this decision.)

作为一个实际问题(在理论和美学问题之外),在任何可以使用的地方使用原语,在不能使用原语的地方使用对象版本。(您已经提到了一个这样的案例。语言和 API 将推动这一决定。)

As a performance freak, you would likely shun the object versions and you may not really care too deeply if you step on a few OO golden rules and sacrosanct no-goes: performance is king and you make your decisions accordingly.

作为一个性能狂热者,您可能会避开对象版本,如果您遵循一些面向对象的黄金法则和神圣不可侵犯的禁忌,您可能不会太在意:性能为王,您相应地做出决定。

I'd recommend option 2 as a good place to start until you develop your own dogmatic preferences! :)

在您发展出自己的教条偏好之前,我建议将选项 2 作为一个很好的起点!:)

回答by mkro

My view: Using Integer as parameters or return values allows one thing that primitive ints don't allow: Using null. But is this a good idea? I think it rarely ever is.

我的观点:使用 Integer 作为参数或返回值允许原始整数不允许的一件事:使用null. 但这是个好主意吗?我认为它很少是。

As far as performance is concerned: The compiler will optimize your code to some degree, so that is most of the time not a real concern.

就性能而言:编译器会在一定程度上优化您的代码,因此大多数情况下这不是真正的问题。