Java Primitive Types: int vs. Integer

前端 未结 6 1394
予麋鹿
予麋鹿 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: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! :)

提交回复
热议问题