Why do people still use primitive types in Java?

后端 未结 21 2502
暗喜
暗喜 2020-11-22 15:11

Since Java 5, we\'ve had boxing/unboxing of primitive types so that int is wrapped to be java.lang.Integer, and so and and so forth.

I see

21条回答
  •  萌比男神i
    2020-11-22 15:17

    1. You need primitives for doing mathematical operations
    2. Primitives takes less memory as answered above and better performing

    You should ask why Class/Object type is required

    Reason for having Object type is to make our life easier when we deal with Collections. Primitives cannot be added directly to List/Map rather you need to write a wrapper class. Readymade Integer kind of Classes helps you here plus it has many utility methods like Integer.pareseInt(str)

提交回复
热议问题