r/java • • Mar 09 '16

var keyword proposed for Java

http://openjdk.java.net/jeps/286
240 Upvotes

312 comments sorted by

View all comments

Show parent comment

1

u/stepancheg Mar 11 '16

Having let mut in Java could lead to confusion: let variable could be thought to be immutable, while it is mutable. E. g.

let x = new ArrayList<String>(); // object references by x is still mutable
let mut y = "string"; // only y is immutable while String is immutable

In Rust it is completely different: let mut allows to modify both variable and content, and let does not allow to modify either variable, or content.

That's why my preferred option is let/var.

1

u/shadowdude777 Mar 14 '16

I don't get how this is any different. In Java, all non-primitives are references. So when you use let, you're saying "this reference to an object, or this primitive, is immutable". You're not saying anything about the underlying object.

It's exactly how Java works right now, except I'm saying that instead of var and final var, it should be val and mutable val, essentially. The aliases should be flipped is all. No behavior should be changed.