Java Primitive Types: int vs. Integer

2020-01-29 08:39发布

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?

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

6条回答
Ridiculous、
2楼-- · 2020-01-29 08:54

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

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

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! :)

查看更多
孤傲高冷的网名
3楼-- · 2020-01-29 09:04

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.

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.

查看更多
乱世女痞
4楼-- · 2020-01-29 09:06

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 unless you need polymorphism, in which case use the corresponding object.

查看更多
够拽才男人
5楼-- · 2020-01-29 09:09

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.

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

查看更多
SAY GOODBYE
6楼-- · 2020-01-29 09:15

In Java, int is a primitive data type, while Integer is a Wrapper class.

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

You can find a more detailed explanation here.

查看更多
孤傲高冷的网名
7楼-- · 2020-01-29 09:16

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

查看更多
登录 后发表回答