5 ms·
Java's `final` while useful for primitives, it is not really that useful for reference types, which constitute the majority of types. C++'s `const` is a much be
by azth 13y ago
Java's `final` while useful for primitives, it is not really that useful for reference types, which constitute the majority of types. C++'s `const` is a much better solution. Better yet, both Rust and D have superior immutability constructs.
- danieldk 13y agoJava's `final` while useful for primitives, it is not really that useful for reference types, which constitute the majority of types. They are if you use them in conjunction with immutable classes, such as Guava's immutable collections. Unfortunately, immutability is only a promise and not typed. But in practice, having a member that is final and of an immutable class works well.
- azth 13y agoYep. The same applies with Scala too (with `val` vs `var` declarations). I still don't like the separation of type declarations with the actual implementation; they should be the same, which is what Rust and even C++ got right.
- danieldk 13y agoWell, with C++'s 'mutable' it's also not much more than a promise ;).
- pron 13y agoImmutability can be typed with Java 8's pluggable type systems . There's an implementation of immutable types in the Checker framework.
- lmm 13y agoSure, but at that point you're not really writing Java any more. If you decide to use Checkers many of Java's plus points (in particular the huge library ecosystem and all the development tools) no longer apply.
- pron 13y agoWell, all reference types are eventually made of primitives -- and arrays. The problem with Java's final is that there are no final arrays. This is planned to be resolved in Java 9.