Immutable objects are thread-safe. When an object is immutable, you cannot make changes to the object. Many threads can read the object at the same time and when a thread needs to change a value, it creates a modified copy. For example, Java strings are immutable. Consider the following code snippet:
import java.util.HashSet;
import java.util.Set;
public class StringsAreImmutable {
public static void main(String[] args) {
String s = "Hi friends!";
s.toUpperCase();
System.out.println(s);
String s1 = s.toUpperCase();
System.out.println(s1);
String s2 = s1;
String s3 = s1;
String s4 = s1;
Set set = new HashSet<String>();
set.add(s);
set.add(s1);
set.add(s2);
set.add(s3);
System.out.println(set);
}
}
Running the code gives the following output:
Hi friends!
HI FRIENDS!
[Hi friends!, HI FRIENDS!]
When...