Saturday, 19 March 2011

Why String is final...

Being final guarantees that instances of String are read-only.(The String class implements read-only objects, but if it were not final it would be possible to write a subclass of String which permitted instances to be changed.) Strings need to be read-only for security ... Security is a more compelling reason. Before String was changed to be final (while Java 1.0 was still in beta) there was a race condition which could be used to subvert security restrictions. It had to do with one thread changing a pathname to a file after another thread had checked that the access was permitted and was about to open it...

1 comment:

  1. In my opinion there are few features as String pool, Security and using as popular hash-map keys are certain reason which is given for string immutability which also held good for final.
    you can also see this article to know Why String is final or immutable in java

    ReplyDelete