Why String class is not Cloneable?

java cloneable example
java cloning interview questions
use of cloneable interface in java
the method clone() from the type object is not visible
java cloneable deep copy
why clone method is protected
extends cloneable
clone() method in java

Why String class is not implementing Cloneable interface?

For example: (We write this type of code sometimes.)

String s1 = new String("Hello");

String s2 = new String("Hello");

Here s1!=s2;

So instead of doing this , if we could have done:

String s1 = new String("Hello");

String s2 = s1.clone();

This could be easy.

The String class represents an immutable string. There would be no purpose to cloning a String. If you feel that you need to clone it, then you can just reuse the same reference and achieve the same effect.

Even if you could clone s1 as s2, then s1 != s2 would still be true. They'd still be references to distinct objects.

Why String class is not Cloneable?, the method clone() from the type object is not visible comparable and cloneable interface java java cloneable deep copy. Why String class is not implementing  Make the class itself Cloneable with a proper public clone method. Don't implement Cloneable but leave the door open for subclasses to do so. In this case you should mimic the behavior of Object.clone by providing a functioning clone method, but leave it as protected and with the throws CloneNotSupportedException declaration.

You can clone string with

String clonedString = new String(stringToClone);

so

String s1 = new String("Hello");
String s2 = new String(s1);

OBJ07-J. Sensitive classes must not let themselves be , When a client requests a String instance by invoking the get() method, the shared A sensitive class that does not implement Cloneable must also follow this  What is Aggregation in Java and why do you need it? How to Convert Int to String in Java? What Is A Virtual Function In Java? Java Regex – What are Regular Expressions and How to Use it? What is PrintWriter in Java and how does it work? All You Need To Know About Wrapper Class In Java : Autoboxing And Unboxing; How to get Date and Time in Java?

Here's another way:

String s2 = s1.concat("");

Java Cloneable interface - Is it broken?, In a Java application, even if a class implements Cloneable interface, we can not make a clone of the class. This itself says a lot about broken  A class implements the Cloneable interface to indicate to the Object.clone() method that it is legal for that method to make a field-for-field copy of instances of that class. Does clone object and original object point to the same location in memory

Cloneable Interface in Java - Object Cloning, public class DogName implements Cloneable { private String dname; public of original object after cloning, the changes does not reflect in the clone object. A class implements the Cloneable interface to indicate to the Object.clone() method that it is legal for that method to make a field-for-field copy of instances of that class. Invoking Object's clone method on an instance that does not implement the Cloneable interface results in the exception CloneNotSupportedException being thrown.

Cloneable (Java Platform SE 7 ), clone() method that it is legal for that method to make a field-for-field copy of instances of that class. Invoking Object's clone method on an instance that does not  Class PrinterMessageFromOperator is a printing attribute class, a text attribute, that provides a message from an operator, system administrator, or "intelligent" process to indicate to the end user information about or status of the printer, such as why it is unavailable or when it is expected to be available.

AutoClone (groovy 2.4.9 API), AutoClone @AutoClone class Person { String first, last List favItems Date since } Properties like first has type String which is not Cloneable so it is left as the  If your class is serializable, you can use serialization for cloning. However, it will come with a performance hit, so do some benchmarking before using serialization for cloning. If you are extending a class and it has defined clone method properly using deep copy, then you can utilize default clone method.

Comments
  • Why would it...? Why would you want to duplicate/clone an immutable object.
  • Please see the updated question.
  • Just do String s2 = s1;. No one can break that.
  • If you think you really have a need for two String references to be different even though the values might be the same, I'd suggest you rethink your design. It probably means you're trying to do something "clever", to save a bit of typing, instead of coding what you mean. And not only is clever code less readable, anyone who sees s1==s2 or s1!=s2 in your code will probably think you made a newbie mistake.
  • It seems that this may be XY problem. Maybe instead of asking about s1.clone(); explain why do you need separate instance of immutable object?
  • But see the other answers: If your solution requires you to clone an immutable object, then your solution probably is some kind of a sneaky/clever trick that will be hard for other programmers to understand. That's almost always a Bad Thing.