Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
Can you please tell me the difference between obj1 = obj.clone() {shallow cloning } and obj1= obj
 
Ranch Hand
Posts: 151
MyEclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
obj1=obj.clone() -- does a shallow cloning
 
Sheriff
Posts: 22791
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;
 
Ranch Hand
Posts: 276
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22791
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Whatever. Here's a tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic