Java 7 Idea: Extensible Strings

Posted by: Joe Walker on April 10, 2007

In some ways it's a shame that java.lang.String is marked 'final' in Java. If it wasn't final, you could inherit from java.lang.String to create strings that had some extra features, or you could extend with a marker interface to declare that a String has some property, for example it would be neat to be able to track if a String had been:

  • Checked for dangerous characters from the web
  • Internationalized
  • Processed by some system

Allowing marker interfaces like this could lead to some nice extra type-checking:

public SafeString checkForUnsafeCharacters(String s) {
  // do some checking and throw or create a SafeString
}

public void process(SafeString s) {
  // do something knowing that the developer can't forget
  // to check the string before it is passed in
}

Marker interfaces on strings can be a handy way to declare that this string has some properly that normal strings don't.

However there are at least 2 good reasons why java.lang.String is final:

  • Security: the system can hand out sensitive bits of read-only information without worrying that they will be altered
  • Performance: immutable data is very useful in making thing thread-safe.

So is it possible to have the best of both worlds - to allow marker interfaces and additional properties without breaking assumptions about the immutability of the string itself. And, is this possible without breaking backwards compatibility?

In Javas 1 to 6, java.lang.String is defined like this:

public final class String {
  public int length() ...
  ... // other methods and properties
}

I think we might be able to safely change this to:

public class String {
  public final int length() ...
  ... // everything made final
}

I don't think this breaks backwards compatibility anywhere because it mostly legalizes something that would previously have been a compile error, and the immutability of the core String is as unbroken as it was in Javas 1-6, although obviously the same guarantees could not be made about all extensions.

Declaring finality in this way allows marker interfaces, which might in some cases allow developers to create APIs that enforce some properties on strings that might previously have been forgotten.

Can anyone think of anything that I've overlooked?

Joe Walker

About Joe Walker

Joe Walker is a developer and consultant working on advanced web development techniques like AJAX.

He recently developed Direct Web Remoting, (DWR) which has become the most popular Ajax toolkit for Java by making browser/server interaction intuitive for web developers. See http://www.directwebremoting.com

He currently works through his consultancy, Getahead (http://getahead.org/), which is supplying a growing number of customers with AJAX and advanced web solutions.

Why Attend the NFJS Tour?

  • » Cutting-Edge Technologies
  • » Agile Practices
  • » Peer Exchange

Current Topics:

  • Languages on the JVM: Scala, Groovy, Clojure
  • Enterprise Java
  • Core Java, Java 8
  • Agility
  • Testing: Geb, Spock, Easyb
  • REST
  • NoSQL: MongoDB, Cassandra
  • Hadoop
  • Spring 4
  • Cloud
  • Automation Tools: Gradle, Git, Jenkins, Sonar
  • HTML5, CSS3, AngularJS, jQuery, Usability
  • Mobile Apps - iPhone and Android
  • More...
Learn More »