From: www on
I wrote an immutable class. By following the samples of tutorials, I
also added "@Immutable" right above the class name.

import net.jcip.annotations.Immutable;

@Immutable //<--- what is this benefit? Without this line, the code is
also ok. Does this line make any difference?
public class MyClass
{
... //all the code
}

Thank you very much.
From: Jim Janney on
www <www(a)nospam.com> writes:

> I wrote an immutable class. By following the samples of tutorials, I
> also added "@Immutable" right above the class name.
>
> import net.jcip.annotations.Immutable;
>
> @Immutable //<--- what is this benefit? Without this line, the code
> is also ok. Does this line make any difference?
> public class MyClass
> {
> ... //all the code
> }
>
> Thank you very much.

There's no immediate effect on the generated code: it simply asserts,
in machine-readable form, the programmer's belief that instances of
the class are immutable. Other Java code may look for this annotation
and possibly make optimizations based on it. A quick Google search
suggests that Findbugs, IntelliJ IDEA, and possibly AspectJ look for
this. I'd call it an interesting idea that hasn't really caught on.

The canonical immutable class, java.lang.String, isn't annotated as
immutable.

--
Jim Janney
From: markspace on
Jim Janney wrote:

> The canonical immutable class, java.lang.String, isn't annotated as
> immutable.


Hmmm:

"Basic, canonic, canonical: reduced to the simplest and most significant
form possible without loss of generality, e.g., "a basic story line"; "a
canonical syllable pattern.""

Not sure if String is "canonical" here. Maybe "ubiquitous" is more what
you're after. Or possibly I just misunderstand what you mean.

From: John B. Matthews on
In article <2p4ofzz6xu.fsf(a)shell.xmission.com>,
Jim Janney <jjanney(a)shell.xmission.com> wrote:

> www <www(a)nospam.com> writes:
>
> > I wrote an immutable class. By following the samples of tutorials, I
> > also added "@Immutable" right above the class name.
> >
> > import net.jcip.annotations.Immutable;
> >
> > @Immutable //<--- what is this benefit? Without this line, the code
> > is also ok. Does this line make any difference?
> > public class MyClass
> > {
> > ... //all the code
> > }
> >
> > Thank you very much.
>
> There's no immediate effect on the generated code: it simply asserts,
> in machine-readable form, the programmer's belief that instances of
> the class are immutable. Other Java code may look for this
> annotation and possibly make optimizations based on it. A quick
> Google search suggests that Findbugs, IntelliJ IDEA, and possibly
> AspectJ look for this. I'd call it an interesting idea that hasn't
> really caught on.

In addition, the API elaborates on this:

<http://www.javaconcurrencyinpractice.com/annotations/doc/net/jcip/annotations/Immutable.html>

For reference, the package summary offers additional perspective, and
it mentions static code-analysis specifically:

<http://www.javaconcurrencyinpractice.com/annotations/doc/net/jcip/annotations/package-summary.html>

> The canonical immutable class, java.lang.String, isn't annotated as
> immutable.

Same for the concrete subclasses of Number.

--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>
From: John B. Matthews on
In article <i1q6ou$a4h$1(a)news.eternal-september.org>,
markspace <nospam(a)nowhere.com> wrote:

> Jim Janney wrote:
>
> > The canonical immutable class, java.lang.String, isn't annotated as
> > immutable.
>
>
> Hmmm:
>
> "Basic, canonic, canonical: reduced to the simplest and most significant
> form possible without loss of generality, e.g., "a basic story line"; "a
> canonical syllable pattern.""
>
> Not sure if String is "canonical" here. Maybe "ubiquitous" is more what
> you're after. Or possibly I just misunderstand what you mean.

Interesting. I took it as suggesting archetypal or prototypical.

Of historical interest: the `Canon` command was used to make the
spelling/case of identifiers uniform throughout a source file:

<http://developer.apple.com/tools/mpw-tools/commandref/canon.html>

--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>