[Java Spec Report] which classes produce immutable objects

"Pat LaVarre" <[email protected]> Tue, 09 Mar 2004 20:09:18 -0000
Newsgroups gmane.comp.java.spec-report
Message-ID <[email protected]>
Re "which classes produce immutable objects", I thought an exchange
from offline might provoke folk here to reply interestingly ...

--- 2004-03-09

> I... say ...
> [some] StringBuffer ... methods ...
> [return immutable] String.

Yes.

> > InformIT ...
> > Once created, some Java objects are
> > "immutable," meaning that they cannot have
> > their contents changed.
> > Which ... classes
> > produce immutable objects? ...
>
> I... say ... StringBuffer ...

No.

You can argue java.lang.StringBuffer, sure.

But then I can argue that methods of java.lang.Math return immutable
primitive values, which are objects without being java.lang.Object's,
thus java.lang.Math "produces immutable objects".

Reductio ad absurdum.

Lay English appearing in a legal document like the JLS would obscure
its meaning, sure.  To make sense of questions like these, we can
develop a _reasonable_ consensus over a legal definition of what the
Java vernacular means by "a class that produces immutable objects".

Here's a beginning of a deductive definition:

"For which types does invoking any constructor return only an instance
that forever remains Object.equals() to its clones."

That formal translation is _only_ a beginning.  I don't know a short
accurate formal definition of "immutable". Object.hashCode() also
matters, an Object can have clones without implementing Cloneable. 
Etc.

Inductively, we can say the autoboxing types are immutable, enums are
immutable (still true as of final draft I think I mis/remember), etc.

Beyond immutability lies the issue of identity, for example we want
all autoboxed 0 to be == not merely Object.equals.  For enums we
demand identity, I think I mis/remember.

Pat LaVarre



To unsubscribe from this mailing list, send an email to:
[email protected]
 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
     http://groups.yahoo.com/group/java-spec-report/

<*> To unsubscribe from this group, send an email to:
     [email protected]

<*> Your use of Yahoo! Groups is subject to:
     http://docs.yahoo.com/info/terms/