From: Jonathan Lee on
On Jul 21, 9:29 am, Lew <no...(a)lewscanon.com> wrote:
> Maybe Java doesn't use a fancy term; when discussing immutability and final we
> just say, "making things final" or "making things immutable" or other phrases
> with the word "final" and "immutable", and everyone understands that we talk
> about that because of the advice to "favor immutability" without having to
> quote that particular chapter title.

This clarifies (for me) a great deal about your earlier posts. Thanks.

--Jonathan
From: Jim Janney on
Jonathan Lee <jonathan.lee.975(a)gmail.com> writes:

> On Jul 20, 9:00 pm, Lew <no...(a)lewscanon.com> wrote:
>> Jonathan Lee wrote:
>> >> I've never heard of an equivalent of "const correctness" in Java,
>> Therefore it doesn't exist?
>
> Er.. no. In fact, I even went out of my way to say that I wasn't
> experienced in Java so people wouldn't draw that conclusion.
>
>>  Anyway, the claim isn't that there's an exact equivalent to
>> "const correctness" but that Java's 'final' has similarities to
>> C++'s 'const', and relative to the Java language fills the same
>> ecological niche.
>
> The OP was asking about const correctness, not the const keyword.
> As for the "final" and "const" keywords being similar... that's
> interesting, but a distinct topic.
>
>> Everyone here seems to think that Java 'final' is just nothing a-'tall like
>> C[++] 'const'.  They are, of course, mistaken.
>
> We don't think that.

I think that, and I've used both C++ and Java for years. Final is a
very limited substitute for const. So is immutability, for that
matter. The closest thing Java can offer is something like
Collections.unmodifiableCollection(), and the language provides no way
for the compiler to enforce that, so it depends on runtime checking.

--
Jim Janney
From: Jim Janney on
Daniel <danielaparker(a)gmail.com> writes:

> On Jul 20, 2:14 pm, Jonathan Lee <jonathan.lee....(a)gmail.com> wrote:
>> On Jul 20, 2:04 pm, Öö Tiib <oot...(a)hot.ee> wrote:
>>
>> I've never heard of an equivalent of "const correctness" in Java,
>> but I also don't use it very much.
>
> Neither Java or C++ has any way of marking a function as "pure".

C++ methods can be declared as const, indicating that they don't
change the state of the objects they are invoked on. There's no
equivalent for this in Java. You might conceivably use an annotation,
but the compiler wouldn't enforce it.

--
Jim Janney

From: Jim Janney on
Jim Janney <jjanney(a)shell.xmission.com> writes:

> Jonathan Lee <jonathan.lee.975(a)gmail.com> writes:
>
>> On Jul 20, 9:00 pm, Lew <no...(a)lewscanon.com> wrote:
>>> Jonathan Lee wrote:
>>> >> I've never heard of an equivalent of "const correctness" in Java,
>>> Therefore it doesn't exist?
>>
>> Er.. no. In fact, I even went out of my way to say that I wasn't
>> experienced in Java so people wouldn't draw that conclusion.
>>
>>>  Anyway, the claim isn't that there's an exact equivalent to
>>> "const correctness" but that Java's 'final' has similarities to
>>> C++'s 'const', and relative to the Java language fills the same
>>> ecological niche.
>>
>> The OP was asking about const correctness, not the const keyword.
>> As for the "final" and "const" keywords being similar... that's
>> interesting, but a distinct topic.
>>
>>> Everyone here seems to think that Java 'final' is just nothing a-'tall like
>>> C[++] 'const'.  They are, of course, mistaken.
>>
>> We don't think that.
>
> I think that, and I've used both C++ and Java for years. Final is a
> very limited substitute for const. So is immutability, for that
> matter. The closest thing Java can offer is something like
> Collections.unmodifiableCollection(), and the language provides no way
> for the compiler to enforce that, so it depends on runtime checking.

I think part of the confusion here is that C++ overloads all its key
words*, so const actually does a number of very different things, only
some of which can be mimicked with final. Rather than saying "Java
doesn't need const correctness, because it has final and immutable
classes", I would say "Java doesn't have statically checked const
correctness, just as it doesn't have multiple inheritance, and for the
most part no one has missed it."

Const correctness is a nuisance because it requires transitive
closure, so that making a single change can ripple through a lot of
other code. And it's confusing, which is why there's so much
discussion on it. Personally, I like it, but I can understand why
some people think it's more trouble than it's worth.

* And if you think const is bad, take a look at static.

--
Jim Janney
From: Peter Duniho on
Jim Janney wrote:
> I think part of the confusion here is that C++ overloads all its key
> words*, so const actually does a number of very different things [...]
>
> * And if you think const is bad, take a look at static.

To be fair, Java does the same thing with both "final" and "static".
Those keywords are context-dependent, just as "const" and "static" are
in C++.