Why do people still use primitive types in Java?

后端 未结 21 2221
暗喜
暗喜 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:15

    Besides performance and memory issues, I'd like to come up with another issue: The List interface would be broken without int.
    The problem is the overloaded remove() method (remove(int) vs. remove(Object)). remove(Integer) would always resolve to calling the latter, so you could not remove an element by index.

    On the other hand, there is a pitfall when trying to add and remove an int:

    final int i = 42;
    final List list = new ArrayList();
    list.add(i); // add(Object)
    list.remove(i); // remove(int) - Ouch!
    

提交回复
热议问题