【问题标题】:Which class constructor is called when we try to create a primitive array object using 'new' [duplicate]当我们尝试使用'new'创建原始数组对象时调用哪个类构造函数[重复]
【发布时间】:2018-11-23 00:34:00
【问题描述】:

当我们尝试执行以下操作时会调用哪个构造函数:

int[] arr = new int[10];

据我了解,每次我们使用关键字 new,都会在 Heap 内创建一个 object,使用相应类的构造函数,其地址为返回到引用变量。

我的问题是;由于 Boolean、byte、short、character、int.... 都是原语并且没有内置类或构造函数(至少我在 src.zip 中找不到),

boolean[] b = new boolean[10];
byte[] by = new byte[10];
short[] s = new short[10];
char[] ch = new char[10];
int[] i = new int[10];
long[] l = new long[10];
float[] f = new float[10];
double[] d = new double[10];

当我们尝试使用new 创建它们的数组时返回什么对象,该对象是如何创建的?

【问题讨论】:

  • 它不是类的实例,而是数组的实例。数组是一种内置数据结构,不提供构造函数。
  • @f1sh 您能否为我提供任何类型的文档以供您回答

标签: java arrays constructor new-operator


【解决方案1】:

array Java 中的一个对象。

基元数组中的值被初始化为其各自的默认值(例如,0 表示 int,0.0 表示 double 等)

对象数组中的值被初始化为指向空引用,直到您分配它们。

【讨论】:

  • 使用哪个类的构造函数?
  • 以前的cmets没看过吗?
  • 我做到了,你能不能给我链接任何关于同一文件的孩子?
  • 如果我没记错的话,其他人已经做过了。
  • 我没看到,你能把它贴在这里吗?
猜你喜欢
  • 1970-01-01
  • 2018-04-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-25
  • 2021-01-20
  • 2013-03-07
  • 2017-01-28
相关资源
最近更新 更多