views:

279

answers:

2

Possible Duplicate:
Why is String final in Java?

There are various moments in my programming life that I wished the the String class had not been final/sealed/NotInheritable.

What are the language architects trying to prevent me from doing that would throw a monkey wrench into the works.

Rather, what are the monkey wrenches the language architects would want to prevent me from throwing into the works by restricting me from extending String class?

Could you list a list of pros-cons of extendable string class?

+4  A: 

String is an immutable class which means if you cannot modify its state after you create it. If you could modify a string after it has entered another library, or a Map for instance the result would be unpredictable.

One mistake of the Java API is that BigInteger and BigDecimal are not final which means you need to perform a defensive copy of these objects when receiving them from non trusted code. Conversely, you can always trust that a String will remain consistent.

Untrustworthy BigInteger:

public class DestructiveBigInteger extends BigInteger {

    public DestructiveBigInteger(String value) {
        super(value);
    }

    public BigInteger add(BigInteger val) {
        return BigInteger.ONE; // add() method does not behave correctly
    }

    public BigInteger subtract(BigInteger val) {
        throw new UnsupportedOperationException("subtract is broken");
    }
}

The same thing is not possible with String. As stated in Effective Java, you need to make defensive copies of these types of objects:

public void setValue(BigInteger value) {
    this.value = new BigInteger(value.toByteArray());
}
krock
Elaborate, pls.
Blessed Geek
Make sure you know the difference between the *final* keyword and the *sealed* keyword. Classes marked *final* are ALSO *effectively sealed*, but that's not the real point of the *final* keyword. A *final* class is **immutable**, meaning it cannot be modified after created. Immutable classes must be also be sealed, otherwise one could inherit, override, and add in some non-immutable behavior.Strings are immutable in Java because mutable strings are **dangerous**, and, frankly, more trouble than they're worth. Most modern languages (Python, Javascript, C#, Java), use immutable strings.
Ender
+3  A: 

String is final because it represents an immutable data type. Manifold terribleness would result from extending String naively because there are lots of libraries that depend upon the immutability of String objects.

Extending String to make it mutable would be invisible to any code the Sting passes through, but would have very surprising and nasty side-effects like suddenly not being able to load values from HashMaps even though you literally have the String of the key since the hashCode would have been hijacked.

Alain O'Dea
If I wanted to use String for keys of a map I would use String. If I wanted to use an extended String as key to a map, that is exactly what I want to do - to prevent it from being accessed with base String objects. Immutability argument does not seem to hold water for me.
Blessed Geek