[HD Pg 98, Sec. 4.2.2 - members-of-an-array-object]
Moderator: admin
-
- Posts: 221
- Joined: Mon Nov 26, 2018 2:43 pm
- Contact:
[HD Pg 98, Sec. 4.2.2 - members-of-an-array-object]
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?
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: [HD Pg 98, Sec. 4.2.2 - members-of-an-array-object]
The para under cloning makes this clear:
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.
There are two points that it has made: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”.
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.
-
- Posts: 221
- Joined: Mon Nov 26, 2018 2:43 pm
- Contact:
Re: [HD Pg 98, Sec. 4.2.2 - members-of-an-array-object]
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?
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?
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: [HD Pg 98, Sec. 4.2.2 - members-of-an-array-object]
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
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?
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.
-
- Posts: 221
- Joined: Mon Nov 26, 2018 2:43 pm
- Contact:
Re: [HD Pg 98, Sec. 4.2.2 - members-of-an-array-object]
Oh ya, what was I thinking! As for same results, I should have said they printed the same results.
Who is online
Users browsing this forum: No registered users and 50 guests