{"id":2807,"date":"2018-04-10T05:07:50","date_gmt":"2018-04-10T05:07:50","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=2807"},"modified":"2018-04-10T05:12:06","modified_gmt":"2018-04-10T05:12:06","slug":"why-clone-works-on-primitive-arrays-and-not-on-object-arrays","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/why-clone-works-on-primitive-arrays-and-not-on-object-arrays\/","title":{"rendered":"Why clone works on primitive arrays and not on Object Arrays"},"content":{"rendered":"<p>When the clone method is invoked upon an array, it returns a reference to a new array which contains (or references) the same elements as the source array.<\/p>\n<p>So in your example, int[] a is a separate object instance created on the heap and int[] b is a separate object instance created on the heap. (Remember all arrays are objects).<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n    int&#x5B;] a = {1,2,3};\r\n    int&#x5B;] b = a.clone();\r\n\r\n    System.out.println(a == b ? &quot;Same Instance&quot;:&quot;Different Instance&quot;);\r\n    \/\/Outputs different instance\r\n<\/pre>\n<p>If were to modify int[] b the changes would not be reflected on int[] a since the two are separate object instances.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nb&#x5B;0] = 5;\r\n    System.out.println(a&#x5B;0]);\r\n    System.out.println(b&#x5B;0]);\r\n    \/\/Outputs: 1\r\n    \/\/         5\r\n<\/pre>\n<p>This becomes slightly more complicated when the source array contains objects. The clone method will return a reference to a new array, which references the same objects as the source array.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nclass Dog{\r\n\r\n        private String name;\r\n\r\n        public Dog(String name) {\r\n            super();\r\n            this.name = name;\r\n        }\r\n\r\n        public String getName() {\r\n            return name;\r\n        }\r\n\r\n        public void setName(String name) {\r\n            this.name = name;\r\n        }\r\n\r\n    }\r\n<\/pre>\n<p>Lets create and populate an array of type Dog<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nDog&#x5B;] myDogs = new Dog&#x5B;4];\r\n\r\n    myDogs&#x5B;0] = new Dog(&quot;Wolf&quot;);\r\n    myDogs&#x5B;1] = new Dog(&quot;Pepper&quot;);\r\n    myDogs&#x5B;2] = new Dog(&quot;Bullet&quot;);\r\n    myDogs&#x5B;3] = new Dog(&quot;Sadie&quot;);\r\n<\/pre>\n<p>Clone dog<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n    Dog&#x5B;] myDogsClone = myDogs.clone();\r\n\r\n<\/pre>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n\r\nSystem.out.println(myDogs&#x5B;0] == myDogsClone&#x5B;0] ? &quot;Same&quot;:&quot;Different&quot;);\r\n    System.out.println(myDogs&#x5B;1] == myDogsClone&#x5B;1] ? &quot;Same&quot;:&quot;Different&quot;);\r\n    System.out.println(myDogs&#x5B;2] == myDogsClone&#x5B;2] ? &quot;Same&quot;:&quot;Different&quot;);\r\n    System.out.println(myDogs&#x5B;3] == myDogsClone&#x5B;3] ? &quot;Same&quot;:&quot;Different&quot;);\r\n<\/pre>\n<p>This means if we modify an object accessed through the cloned array, the changes will be reflected when we access the same object in the source array, since they point to the same reference.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nmyDogsClone&#x5B;0].setName(&quot;Ruff&quot;); \r\n    System.out.println(myDogs&#x5B;0].getName());\r\n    \/\/Outputs Ruff\r\n<\/pre>\n<p>changes to the array itself will only affect that array.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nmyDogsClone&#x5B;1] = new Dog(&quot;Spot&quot;);\r\n    System.out.println(myDogsClone&#x5B;1].getName());\r\n    System.out.println(myDogs&#x5B;1].getName());\r\n    \/\/Outputs Spot\r\n    \/\/Pepper\r\n<\/pre>\n<p>As you see above the change in the child by assigning a new Dog does not have any impact on the parent because the reference itself is changed instead of change in the reference value.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When the clone method is invoked upon an array, it returns a reference to a new array which contains (or references) the same elements as the source array. So in your example, int[] a is a separate object instance created on the heap and int[] b is a separate object instance created on the heap.&hellip; <a href=\"https:\/\/codethataint.com\/blog\/why-clone-works-on-primitive-arrays-and-not-on-object-arrays\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-2807","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/2807","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=2807"}],"version-history":[{"count":1,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/2807\/revisions"}],"predecessor-version":[{"id":2808,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/2807\/revisions\/2808"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=2807"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=2807"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=2807"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}