Why do people still use primitive types in Java?

后端 未结 21 2200
暗喜
暗喜 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条回答
  •  囚心锁ツ
    2020-11-22 15:24

    The primitive types are much faster and require much less memory. Therefore, we might want to prefer using them.

    On the other hand, current Java language specification doesn’t allow usage of primitive types in the parameterized types (generics), in the Java collections or the Reflection API.

    When our application needs collections with a big number of elements, we should consider using arrays with as more “economical” type as possible.

    *For detailed info see the source: https://www.baeldung.com/java-primitives-vs-objects

提交回复
热议问题