When to use primitive and when reference types in Java

后端 未结 11 526
旧时难觅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:34

    That really depends on the context. First prefer the primitive, because it's more intuitive and has less overhead. If it is not possible for generics/autoboxing reasons, or if you want it to be nullable, then go for the wrapper type (complex type as you call it).

提交回复
热议问题