Comparing Performance of int and Integer

后端 未结 5 2081
天涯浪人
天涯浪人 2020-12-18 18:26

Which one is best in programming - int or Integer? Especially whenever both are doing the same task?

I am writing an appli

5条回答
  •  离开以前
    2020-12-18 19:04

    Use int wherever possible. Use Integer only if:

    • You need a way to represent "no value" or "uninitialized". Integer can be null, an int always has an int value.
    • You want to use collections like List (though you can use int externally and have autoboxing hide the fact that the collection internally stores Integer instances).
    • You're using an API, framework or tool that requires you to use Integer or Object, or doesn't work with primitive types.
    • You need to be able to synchronize on the object (very unlikely).

提交回复
热议问题