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.
***“非原始”类型被官方称为引用类型。
5 个解决方案
#1
20
Short answer: An int
is a number; an Integer
is a pointer that can reference an object that contains a number. Using Integer
for arithmetic involves more CPU cycles and consumes more memory. An int
is not an object and cannot passed to any method that requires objects (just like what you said about Generics).
简短回答:int是一个数字;整数是一个可以引用包含数字的对象的指针。使用Integer进行算术会占用更多CPU周期并消耗更多内存。 int不是一个对象,不能传递给任何需要对象的方法(就像你对Generics所说的那样)。
#2
4
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.
非基元类型是对象。必须动态分配,垃圾收集和检查null-ness(尽管其中一些操作可能会被优化编译器删除)。读取它们的实际值需要从指针加载。原始类型是值。它们通常占用较少的空间并且访问速度更快。
A good rule of thumb is, use primitive types unless you need polymorphism, in which case use the corresponding object.
一个好的经验法则是,使用原始类型,除非你需要多态,在这种情况下使用相应的对象。
#3
3
There is a slight penalty for converting between the types (autoboxing). Also int
will have a bit less overhead so I would always go with int
if you can.
在类型之间进行转换(自动装箱)会有轻微的损失。 int也会有更少的开销,所以如果可以的话我会一直使用int。
Also see this question: When to use primitive and when reference types in Java
另请参阅此问题:何时使用原语和Java中的引用类型
#4
2
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.
作为一个表演怪胎,你可能会避开对象版本,如果你踩到一些OO黄金规则和神圣不苟的话,你可能不会真的太在意:表现为王,你做出相应的决定。
I'd recommend option 2 as a good place to start until you develop your own dogmatic preferences! :)
我建议选择2作为一个好的开始,直到你开发自己的教条偏好! :)
#5
0
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作为参数或返回值允许原始int不允许的一件事:使用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.
就性能而言:编译器会在一定程度上优化您的代码,因此大多数时候这不是真正的问题。
#1
20
Short answer: An int
is a number; an Integer
is a pointer that can reference an object that contains a number. Using Integer
for arithmetic involves more CPU cycles and consumes more memory. An int
is not an object and cannot passed to any method that requires objects (just like what you said about Generics).
简短回答:int是一个数字;整数是一个可以引用包含数字的对象的指针。使用Integer进行算术会占用更多CPU周期并消耗更多内存。 int不是一个对象,不能传递给任何需要对象的方法(就像你对Generics所说的那样)。
#2
4
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.
非基元类型是对象。必须动态分配,垃圾收集和检查null-ness(尽管其中一些操作可能会被优化编译器删除)。读取它们的实际值需要从指针加载。原始类型是值。它们通常占用较少的空间并且访问速度更快。
A good rule of thumb is, use primitive types unless you need polymorphism, in which case use the corresponding object.
一个好的经验法则是,使用原始类型,除非你需要多态,在这种情况下使用相应的对象。
#3
3
There is a slight penalty for converting between the types (autoboxing). Also int
will have a bit less overhead so I would always go with int
if you can.
在类型之间进行转换(自动装箱)会有轻微的损失。 int也会有更少的开销,所以如果可以的话我会一直使用int。
Also see this question: When to use primitive and when reference types in Java
另请参阅此问题:何时使用原语和Java中的引用类型
#4
2
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.
作为一个表演怪胎,你可能会避开对象版本,如果你踩到一些OO黄金规则和神圣不苟的话,你可能不会真的太在意:表现为王,你做出相应的决定。
I'd recommend option 2 as a good place to start until you develop your own dogmatic preferences! :)
我建议选择2作为一个好的开始,直到你开发自己的教条偏好! :)
#5
0
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作为参数或返回值允许原始int不允许的一件事:使用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.
就性能而言:编译器会在一定程度上优化您的代码,因此大多数时候这不是真正的问题。