Java 10 brings a C#-like var
keyword for local type-inference.
But does Java 10 also provide a val
keyword, as is found in Scala?
val
would work like var
but the binding would be final
.
var x = "Hello, world. ";
x = "abc"; // allowed
val y = "Hello, world. ";
y = "abc"; // forbidden
If it does not, is there a reason that this is the case?
If you want to use "val", that is, "final var", you always can to use Lombok's val.
Because there is
final var
for that in Java. If we hadval
too, there would be two things that mean the same. This is not good. There should be only one way to express a particular thing.There is no
val
in Java 10, as stated in JEP 286: Local-Variable Type Inference:And here's the main reasoning:
(Source)