[HD Pg 98, Sec. 4.2.2 - members-of-an-array-object]

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

[HD Pg 98, Sec. 4.2.2 - members-of-an-array-object]

Post by OCAJO1 »

In the section talking about Cloning Arrays, it talks about cloning String arrays does not create a new object on the heap. Is this only a property of String arrays or any object array?

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 98, Sec. 4.2.2 - members-of-an-array-object]

Post by admin »

The para under cloning makes this clear:
This method creates a copy of the array object. Note that it doesn’t create copies of the objects referred to by the array elements. It merely creates a new array object of the same length and copies the contents of existing array into the new array. Which means, if the existing array contained primitive values, those values will be copied to the elements of the new array. If the existing array contained references to objects, those references will be copied to the elements of the new array. Thus, the elements of the new array will also point to the same objects. This is also known as “shallow copy”.
There are two points that it has made:
1. This method creates a copy of the array object. (Since objects are always on the heap, this new array object will be on the heap. Explained in previous chapters).
2. It merely creates a new array object of the same length and copies the contents of existing array into the new array.
If you are clear about the what a reference is (explained in previous chapters), you will know what the elements of an array of references will actually be.

The example illustrates this with a int array and a String array. The String example is actually an example about array of references, so an array of references of any type will work the same way. This has nothing to do with String in particular.
If you like our products and services, please help us by posting your review here.

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

Re: [HD Pg 98, Sec. 4.2.2 - members-of-an-array-object]

Post by OCAJO1 »

One more question about cloning an array,

Given either example:

Object[] icc[] = { {"string is also an object."}, {null } , { new Object() , new Integer(10)} };

or

int[][] iaa = new int[][]{ new int[]{ 1, 2, 3 }, {4, 5, 6} };

Since

Object[] idd[] = icc; and int[][] ibb = iaa; produce the same results as

Object[] idd[] = (Object [][]) icc.clone(); and int[][] ibb = (int [][]) iaa.clone();

why use clone() at all?

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 98, Sec. 4.2.2 - members-of-an-array-object]

Post by admin »

Well, it is as if you haven't read the Object and Reference (Section 1.4) and the cloning discussion under 4.2.2 (observe the diagram carefully) at all :D

You would use cloning when you want to have a new array object and not merely a reference to the same array object! Once you have a new array object, you could potentially change its members to have values that are different from the original array object.

Also, when you say, "produce the same results as", what tests did you run to arrive at the conclusion that they produce the same result?
If you like our products and services, please help us by posting your review here.

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

Re: [HD Pg 98, Sec. 4.2.2 - members-of-an-array-object]

Post by OCAJO1 »

Oh ya, what was I thinking! As for same results, I should have said they printed the same results.

Post Reply

Who is online

Users browsing this forum: No registered users and 27 guests