简体   繁体   English

Primitive array和Reference Array有什么区别?

[英]What is the difference Between Primitive array and Array of Reference .

I read in net and Found reference array store references. 我阅读了net和Found引用数组存储引用。 References in sense the array is going to store memory address of variables i Guess if i am not mistaken. 从某种意义上说,引用是要存储变量的内存地址,我猜如果我没记错的话。 If that's the Case why i don't see the memory address when i loop through string array as Below. 如果是这种情况,为什么当我遍历如下所示的字符串数组时看不到内存地址。

  String[] arrNames = new String[3];
  arrNames[0]       = "John";
  arrNames[1]       = "Mac";
  arrNames[2]       = "Alex";

Now as per the definition the arrNames array is going to store References at arrNames[0],arrNames[1], arrNames[2]. 现在按照定义,arrNames数组将把引用存储在arrNames [0],arrNames [1],arrNames [2]处。 Which means memory address which is going to point to Names i.eJohn, Max and Alex. 这意味着将指向名称(即John,Max和Alex)的内存地址。

If it is Primitive array its directly going to store the values like below. 如果它是原始数组,则将直接存储如下所示的值。

 int[] Num = new int[3];
 Num[0]    = 1;
 Num[1]    = 2;
 Num[2]    = 3;

The Num[0] is directly going to hold Numbers 1 instead of address which points to number. Num [0]直接保留数字1而不是指向数字的地址。

Please correct me if i misunderstood it. 如果我误会了请纠正我。

In java there is no primitive array. 在Java中,没有原始数组。 Even though we had the primitive values in an array, then the array itself considered as array object. 即使我们在数组中具有原始值,但数组本身仍被视为数组对象。

Primitive arrays and Reference arrays are exactly similar object. 基本数组和引用数组是完全相似的对象。

Moreover, default values also applied with a primitive array: 此外,默认值也适用于原始数组:

int[] myPrimitiveArray = new int[1];

assertTrue(myPrimitiveArray[0], 0)     //passed since 0 by default in each cell

Same as: 如同:

Integer[] myReferenceArray = new Integer[1];

assertTrue(myPrimitiveArray[0], 0)     //passed since 0 by default in each cell

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM