Better HN
topnewbestaskshowjobs
Log inSign up

ceedubs

2 karma · joined Nov 17, 2011 (15y ago) · 1 submissions

Submissions

comment 13y ago · link · parent

The article confuses immutability with being 'final' (both in Java and in Scala).

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")