Java Primitive Types: int vs. Integer

前端 未结 6 1360
予麋鹿
予麋鹿 2020-12-03 14:02

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 exampl

相关标签:
6条回答
  • 2020-12-03 14:30

    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.

    0 讨论(0)
  • 2020-12-03 14:33

    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.

    0 讨论(0)
  • 2020-12-03 14:36

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

    0 讨论(0)
  • 2020-12-03 14:40

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

    0 讨论(0)
  • 2020-12-03 14:45

    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

    0 讨论(0)
  • 2020-12-03 14:47

    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.

    0 讨论(0)
提交回复
热议问题