When to use primitive and when reference types in Java

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

    I dont think there is any rule as such. I would choose the types over primitives (Integer over int) when I write method signatures, Maps, collections, Data Objects that get passed around. As such I would still like to use Integer instead of int even inside of methods etc. But if you think that is too much trouble (to type extra "eger") then it is okay to use ints for local variables.

提交回复
热议问题