7
import java.util.*;
import java.lang.*;

public class Test{
    public static void main(String[] argv){
        String s1="abc";
        String s2=(String) s1.clone();
    }    
}

Why this simple test program doesn't work?

Danielson
  • 2,605
  • 2
  • 28
  • 51
user1192813
  • 71
  • 1
  • 1
  • 2
  • 6
    In what way doesn't it work? – Jivings Feb 06 '12 at 17:10
  • Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - unreported exception java.lang.CloneNotSupportedException; must be caught or declared to be thrown at test.Test.main(Test.java:9) Java Result: 1 – user1192813 Feb 06 '12 at 17:12

5 Answers5

20

clone is a method of the Object class. For a class to be "cloneable" it should implement the marker Cloneable interface. String class doesn't implement this interface and doesn't override the clone method hence the error.

I hope the above snippet is for educational purposes because you should never feel a need to call clone on strings in Java given that:

  1. Strings in Java are immutable. Feel free to share them across methods/classes
  2. There already exists a constructor new String(String) which acts like a copy constructor and is pretty much equivalent to your clone() call.
Sanjay T. Sharma
  • 22,857
  • 4
  • 59
  • 71
6

Object.clone() is protected. It is a tricky API to use.

Usually one exposes clone() when one extends Object by broadening the method's visibility.

Clone on any string has little meaning, since it is both final and immutable.

There is a reason to copy a string; that can be done with:

String s1 = ...;
String s2 = new String(s1)
Dilum Ranatunga
  • 13,254
  • 3
  • 41
  • 52
  • 1
    It's rare to need to copy a string, and you haven't explained why one would ([this question](http://stackoverflow.com/questions/390703/what-is-the-purpose-of-the-expression-new-string-in-java) does). – Matthew Flaschen Feb 06 '12 at 17:17
1

clone() is a protected method on the Object class. If you want a class to be cloneable the general pattern is to implement Cloneable and make that method public.

driangle
  • 11,601
  • 5
  • 47
  • 54
1

It obviously couldn't be compiled. Object.clone has protected access.

Beyond being accessible within the class itself and to code within the same package..., a protected member can also be accessed from a class through object references that are of at least the same type as the class

0

For a class to be "cloneable" it should implement the marker Cloneable interface. String class doesn't implement this interface and doesn't override the clone method hence the error.

protected Object clone() throws CloneNotSupportedException creates and returns the exact copy (clone) of this object.

Strings in Java are immutable. Feel free to share them across methods/classes There already exists a constructor new String(String) which acts like a copy constructor and is pretty much equivalent to your clone() call.

Usually one exposes clone() when one extends Object by broadening the method's visibility.

Clone on any string has little meaning, since it is both final and immutable.

VicXj
  • 165
  • 1
  • 6