Java Beginner
Java Beginner

Reputation: 1655

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

I read in net and Found reference array store references. 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]. Which means memory address which is going to point to Names i.eJohn, Max and 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.

Please correct me if i misunderstood it.

Upvotes: 0

Views: 1704

Answers (2)

Mik378
Mik378

Reputation: 22171

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

Upvotes: 0

MGPJ
MGPJ

Reputation: 1072

In java there is no primitive array. Even though we had the primitive values in an array, then the array itself considered as array object.

Upvotes: 1

Related Questions