Type inference in newer languages reduces a lot of the pain that was Java.
Of course, it’s not new but adoption took some time
I also like to explicitly declare immutability/ mutability
let x = “hello world”
var ctr = 0
Of course, it’s not new but adoption took some time
I also like to explicitly declare immutability/ mutability
let x = “hello world”
var ctr = 0
Scala takes this approach, using var for declaring mutable variables, and val for declaring immutables. In my experience, in well designed imperative code, only a small proportion of variables need mutation, so using keywords like const and final is backward. It would make more sense to use a mutable keyword, or to do as Scala does. (C++ has a mutable keyword, but it's for a different purpose.)
https://docs.scala-lang.org/overviews/scala-book/two-types-v...
Little things like this make a language so much more pleasant.
var mutableVar = "Foo"
final var immutableVar = "Bar" 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.