4 ms·
> What about final for Java function parameters? For built-ins, such as numeric types and "char" (String is obviously disallowed from this definition), final w
by AdieuToLogic 11y ago
> What about final for Java function parameters?
For built-ins, such as numeric types and "char" (String is obviously disallowed from this definition), final will prevent changes to instances of those types similar to what "const int" does for C++.
For pointers, however, the final keyword in Java limits the ability to change the variable holding the pointer, not to what it references. The equivalent in C++ would be:
SomeClass * const foo = &instanceOfSomeClass;
Here, the pointer "foo" could not change, but operations changing foo would be allowed.
Java (the language) does not possess the semantics to express "the pointer given to a method cannot allow changes to what it points."
- EdwardDiego 11y agoYep, if you want an immutable list, or whatever, you have to make sure your list is immutable, which usually involves defensive copying.