by ceedubs · 13y ago · 0 comments
From the article:"In Java you can use the final keyword to differentiate variables that are not mutable. Scala has two keywords val and var to differentiate between mutable and immutable types."
If a variable is 'final' in Java, then the variable cannot be reassigned to another object once initialized. This does not at all prevent it from being used for a mutable object. For example:
final Map<String, String> stateToCapital = new HashMap<String, String>();
// since HashMap is a mutable type, I can add a key/value pair:
stateToCapital.put("Indiana", "Indianapolis");
Similarly in Scala, I could make a 'final' ('val' as opposed to 'var') assignment on a mutable data type: val myMutableMap = collection.mutable.Map("Indiana" -> "Indianapolis")No comments yet.