Being Java programmer.
Being Java programmer.
Ganish Patil wrote:I just want to know does cloning creates new another object or just both reference variables here newKeywordObj1 and shallowCopyObj share the same object?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
First time came to know, thought Class.forName() and clone methods etc. create an object but now got to know about factory methods. Yes I got the concept of shallow copy and deep copy.Winston Gutkowski wrote: the ONLY way to create an object in Java is with the new keyword.
Being Java programmer.
Ganish Patil wrote:But beneath in my new program
Wrong answer I got: When I change the value of 0th element of int[] marks of object myClsDeepCopyObj1 to 35 it is also changing the value of 0th element of int[] marks of object myClsNewKeywordObj1 which actually should be 80.
Correct answer I got of: When I changed the value of age variable of object myClsDeepCopyObj1 to 25 then only it's value is changed not myClsNewKeywordObj1. I also got about AtomicInteger idNumber as it is object reference so both points to same reference when changed both prints same value 201.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote: When Java clones arrays, it creates a "shallow" copy, which is fine for primitives, and things like String[]
because when I changed value of element at 1th location of int numbercopy[] doesn't change value at index 1 of int number[]. so they have different references means this clone copied complete object not reference, hope I'm on right pathOutput:
Original
1
2
3
Cloned
1
11
3
Being Java programmer.
Ganish Patil wrote:
Winston Gutkowski wrote: When Java clones arrays, it creates a "shallow" copy, which is fine for primitives, and things like String[]
but this code is doing deep copy I think am I correct ?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
then array of int elements i.e. primitives in my main program is not showing different outputs as below code showsNo, The reason it "works" is that ints are primitives, so when they are copied, you get a whole new value that is a copy of the original. And you'll get that whether your clone is "shallow" or "deep".
I wrote another program so will be easy to understand what I'm not getting, here below is program like same above code just wrote in separate class. but when changed value of 1th index of array the output of both object's myNumbers[1] is same i.e. 300. Why this one is not different like myNumbers[1] of newKeywordclnObj1 should be 2 and only myNumbers[1] of deepCpyObj1 should be 300 as we got in above code?Output:
Original
1
2
3
Cloned
1
11
3
Being Java programmer.
Ganish Patil wrote:Last doubt....I wrote another program so will be easy to understand what I'm not getting,..Why this one is not different like myNumbers[1] of newKeywordclnObj1 should be 2 and only myNumbers[1] of deepCpyObj1 should be 300 as we got in above code?[/b]
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
yes got that now, both works very well as I expected. Thank you so much. Now I'm clear with these two concepts.Winston Gutkowski wrote:Note: this is one case where you can't use a "getter". You must assign the value directly to the member field.
Being Java programmer.
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |