When to use primitive and when reference types in Java

后端 未结 11 541
旧时难觅i
旧时难觅i 2020-12-01 02:55

In which case should you use primitive types(int) or reference types (Integer)?

This question sparked my curiosity.

11条回答
  •  暖寄归人
    2020-12-01 03:35

    One case in which Integer might be prefered is when you are working with a database where numerical entries are allowed to be null, since you wouldn't be able to represent a null value with an int.

    But of course if you're doing straight math, then int would be better as others have mentioned due to intuitiveness and less overhead.

提交回复
热议问题