• 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

Cloning Objects of String and Integer classes

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
While working on an issue recently, I came across a problem where I thought that cloning a hashmap(having some integers and strings) would solve the problem.
However when I read more about what the method does, I found that it does a shallow copy of hashmap object and the objects are not actually copied.
Hence I thought to iterate through the hashmap and call clone of each key-value pair, but i found out that String and Integers (in fact all the wrapper classes) doesn't support clone.

My question is - why these classes don’t support clone method and what are the other intelligent ways to clone object of these classes?

Also it seems weird that clone method is object class and not in Cloneable object.
What is the reason behind this design decision?

 
Marshal
Posts: 79036
375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, it might be more sensible to have the clone method as part of a Cloneable interface, but it is too late to change now.
Integer, String etc don't need to be cloned because they are immutable classes.
 
Sheriff
Posts: 22780
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Swapnil Shroff wrote:Also it seems weird that clone method is object class and not in Cloneable object.
What is the reason behind this design decision?


The base of just about all clone() implementations is a call to super.clone(). That is only possible if all classes have the clone() method, and that can only be done by putting it in Object.
Cloneable could have had clone() as well, but because it's an interface that would require the clone() method to become public. For some reason Sun decided that would be a bad idea. Possibly because that way you cannot write clone() methods that should be used for internal use only.
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Swapnil Shroff wrote:My question is - why these classes don’t support clone method and what are the other intelligent ways to clone object of these classes?


The String and wrapper classes are immutable (read: the objects of this class can't change). When you want to clone an object of this class just use the reference
to the original object. No operation can change the value so there will be no problem that you have 2 references to the same object.
 
Ranch Hand
Posts: 449
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:because it's an interface that would require the clone() method to become public. For some reason Sun decided that would be a bad idea. Possibly because that way you cannot write clone() methods that should be used for internal use only.


I found this bug. http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4098033.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic