gpt4 book ai didi

java - Java 中原始数据类型和引用数据类型的共存

转载 作者:行者123 更新时间:2023-12-01 22:25:26 24 4
gpt4 key购买 nike

我总是问自己这个问题:为什么 Java 设计者在他们的语言中引入了原始类型和引用类型。换句话说,为什么会存在两种可以实现相同目标的数据类型,例如(int&java.lang.Integer),(float&java.lang.Float)...有人可以解释一下这个问题吗?

最佳答案

Java 具有原语是因为:

  1. They are fast. (when compared to Objects)
  2. They have less overhead. (when compared to Objects)
  3. They actually make life easier for people with C/C++ background and gives them the same feel (almost).

Java 有包装器,因为:

  1. In certain data structures like Collections, only objects are allowed to be added because when doing Garbage Collection, the GC treats all these things only as Objects and then performs operations on them.

  2. Using Wrappers (Objects) instead of primitives in Collections is more of a design choice because it allows general behavior of methods. For example equals() , contains() on collections work on the basis of method overriding which cannot be done on primitives.

关于java - Java 中原始数据类型和引用数据类型的共存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28876543/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com