• Post Reply Bookmark Topic Watch Topic
  • New Topic

Difference between obj1 = obj.clone() and obj1= obj  RSS feed

 
Minni Sharma
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
Can you please tell me the difference between obj1 = obj.clone() {shallow cloning } and obj1= obj
 
akhter wahab
Ranch Hand
Posts: 151
Java MyEclipse IDE Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
as i know obj1=obj.clone() in this a new obj1 will be made in the memory same as the obj
and by doing obj1=obj a new reference of obj is made named as obj1 in the memory new obj will not be made it will point the old obj ..

please consider my answer after confirming from at least one person more
 
Rajesh Nagaraju
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
obj1=obj.clone() -- does a shallow cloning
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In most cases obj.clone() indeed returns a clone, or copy. This may be a shallow clone (by default), or a deep clone (if clone() has been overridden to clone all internals as well).

I said "in most cases" because technically it's allowed that obj1.clone() == obj1. This hardly ever occurs though; I still have to see the first occurrence in real code.
 
Naman Patidar
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Rob : obj1.clone()==obj1 will always return false. colne() method creates a bit-by-bit copy of object so the objects will have different hashcodes.
@ minni :
obj1 = obj.clone() {shallow cloning } : this will create a copy of obj object (a new object altogether) and assign it to obj1.
obj1= obj : this will assign same obj object to obj1;
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if object.clone() does a bit by bit copy, then mustn't the hashcode values of both be same?! Just asking, I'm not sure though.
 
Naman Patidar
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bit-by-bit copy means copy of object itself not of reference.

Object obj = new Object();
obj => is reference, pointing to actual object created by "new Object()".
reference contains a hashcode/value which in turn point to real object.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Naman Patidar wrote:@Rob : obj1.clone()==obj1 will always return false. colne() method creates a bit-by-bit copy of object so the objects will have different hashcodes.

Not necessarily. The API clearly states:
Creates and returns a copy of this object. The precise meaning of "copy" may depend on the class of the object. The general intent is that, for any object x, the expression:will be true, and that the expression:will be true, but these are not absolute requirements. While it is typically the case that:will be true, this is not an absolute requirement.

The following are therefore all valid implementations:
However, it is very usual to use super.clone(), and if all classes up the chain do that then yes, obj1.clone() != obj1 and all fields are copied.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!