5 ms·
Since Java 10: var mutableVar = "Foo" final var immutableVar = "Bar"
by asn0 5y ago
Since Java 10:
var mutableVar = "Foo"
final var immutableVar = "Bar"
- dtech 5y agoFinal has been in Java for a lot longer, but you're also missing the point. Immutability should be the default, or at least not a secondary citizen to the dangerous and often unnecessary mutability.
- imtringued 5y agoYes, I still remember how in Ceylon they just did "value x = bla" and the variable was immutable by default. If you wanted to actually make it mutable you had to write it out as "variable Type x = a" to make it stand out and be really annoying to have mutable variables.
- kaba0 5y agoWhich is the road taken by record classes and the upcoming primitives.
- cloogshicer 5y agoThis is not immutability, final only prevents re-assignment, not mutation: final ArrayList<String> foo = new ArrayList<>(); foo.add("Foo"); // Compiles and runs - this is mutation foo = new ArrayList<>(); // Compiler error - this is re-assignment In your example, both Strings are immutable, since Java Strings are always immutable.
- imtringued 5y agoThe variable is immutable.