Posts

Showing posts from July, 2015

The Curious Case Of Strings In Java

Image
img src The most important thing to note about String objects in java is that they are immutable. Immutability means that once the state of an object is defined, it cannot be changed. To make String immutable, all the attributes in String class are declared as final. The attribute 'hash' holding the hashcode is an exception though. Following extract from the String class shows the declaration of the attribute which holds the String's value 1 2 /** The value is used for character storage. */ private final char value []; Because this char array is declared as final, once a String object is assigned a value, it can never be changed. However, the references to a String object are mutable, which means , we can point to another String object using the same reference. Let's look at the following code which explains the above concept : 1 2 3 4 5 6 7 8 9 10 11 12 13 public class TestStrings { public static void ...

What is ConcurrentModificationException , how is it caused and how can it be prevented ? (Part 2- ArrayLists)

In the previous article, we saw with an example of HashMap  how a ConcurrentModificationException is caused. In this article ,we will see how it happens in case of Lists. The basics are the same as in the example of a HashMap. When the structure of a list is modified by adding or removing an element from the list while an iterator is  traversing over it, a ConcurrentModificationException is caused. This is because just like HashMap , an  ArrayList also maintains a count of the times the structure of the list is modified (modCount). When an  iterator is obtained on that list, it creates its own expectedCount which is set to the modCount value . Any  addition or removal from the list will change the modCount.When the iterator compares the modCount with  expectedCount whenever iterator's next() method is called , it throws the  ConcurrentModification exception  if it finds a difference .Such an iterator is said to be fail-fast. When we use Cop...

What is ConcurrentModificationException , how is it caused and how can it be prevented ? (Part 1- HashMaps)

ConcurrentModificationException occurs when a Collection is modified structurally while an iterator is already traversing the Collection. Taking the example of a HashMap,while iterating through the entries of a HashMap, if the internal structure of the HashMap is changed by either adding an element to it or removing an element from it, a ConcurrentModificationException is caused. What happens internally? HashMap maintains an integer variable called modCount. This modCount is nothing but the count of times the HashMap has been modified structurally. If we add an element to the HashMap, the modCount increases. Similarly, if we remove an element from the HashMap, the modCount increases. Interestingly, when we just update an entry, there isn't any change to the modCount. This is because update to an element does not change the internal structure of the HashMap. When we create an iterator for the Entry table of the HashMap, an expectedCount variable is created which is s...