Re: How do java programmers cope with java missing c++ const?



Michael Redlich sez:

josh.s17@xxxxxxxxx wrote:
I've been working with java for a while now, but I miss the ability to
pass around objects that can't be modified, as is possible with the c++
const keyword. Is it just me or do others find this a limitation of
java? I've read the way to do it in java is to pass around immutable
objects however that seems a lot less flexible than having the ability
to pass a const reference to a mutable object as can be done in c++.
....
In any event, you could do similar things in Java using the 'final'
keyword. It is a modifier used to change the characteristics to
classes, methods, field, and parameters so that those items cannot be
changed.

Except of course if the parameter is an object, it is actually
a pointer. So 1) final'ing the pointer does not prevent changes
to the actual object, and 2) since pointer is passed by value,
changing it does not change the object, and any such change is
lost upon exit from subroutine. IOW, it isn't clear exactly
what the point of declaring parameters "final" is: it doesn't
do anything actually useful.

Dima
--
Sufficiently advanced incompetence is indistinguishable from malice.
.



Relevant Pages

  • Re: 406 Not Acceptable (was Re: "--All You Zombies--" title)
    ... The semantics and proper usage of the const keyword. ... The semantics and proper usage of the volatile keyword. ... The difference between an array and a pointer. ... Just check the output from cfront for abuse of the ',' operator. ...
    (rec.arts.sf.written)
  • Re: 406 Not Acceptable (was Re: "--All You Zombies--" title)
    ... The semantics and proper usage of the const keyword. ... The semantics and proper usage of the volatile keyword. ... The difference between an array and a pointer. ...
    (rec.arts.sf.written)
  • Re: 406 Not Acceptable (was Re: "--All You Zombies--" title)
    ... The semantics and proper usage of the const keyword. ... The semantics and proper usage of the volatile keyword. ... The difference between an array and a pointer. ...
    (rec.arts.sf.written)
  • Re: Copying pointers
    ... observe the change through either pointer. ... overcome your original confusion, because you are still talking about ... "the original pointer gets modifier". ...
    (comp.lang.c)
  • Re: help with left-right cdecl rule
    ... I am having trouble understanding how the 'const' modifier affects the ... 'left-right' rule when deciphering c-declarations: ... x is pointer to a const int meaning you have a modifiable pointer but ... and one is making a pointer to a const int. ...
    (comp.lang.c)