[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

the benefits of immutability



On 09 Aug 2003 14:20:45 -0400 (Message-ID:
<87bruyofpe.fsf@snark.piermont.com>), Perry E. Metzger wrote:

 > I recently wrote a (partial) Scheme interpreter in Java to learn
 > Java better, but part of what I learned what how limiting Java
 > is. Not having function pointers sucks big time, as does not being
 > able to subclass a class like String

The finality of the String class is a consequence of its immutability.
One could argue that immutable classes are a good thing for most of
the same reasons why functional programming is a good thing.  No side
effects means a lot fewer things to worry about.  For example,
immutable classes are thread-safe.

With the availability of StringBuffer as a mutable companion of
String, I honestly can't remember when was the last time I wished
String were not final.

As for the perceived lack of function pointers in Java, the complaint
is somewhat justified, although not entirely accurate.  While Java is
no Lisp, it does have function objects.  Here's one:

http://java.sun.com/j2se/1.3/docs/api/java/lang/String.html#CASE_INSENSITIVE_ORDER

Here's an example of a method one of whose parameters is a function:

http://java.sun.com/j2se/1.3/docs/api/java/util/Arrays.html#sort(java.lang.Object[],%20java.util.Comparator)