View Khushi   Kumari 's Profile
Why String class is final or immutable?
It is very useful to have strings implemented as final or immutable objects. Below are some advantages of String Immutability in Java

Immutable objects are thread-safe. Two threads can both work on an immutable object at the same time without any possibility of conflict.
Security: the system can pass on sensitive bits of read-only information without worrying that it will be altered
You can share duplicates by pointing them to a single instance.
You can create substrings without copying. You just create a pointer into an existing base String guaranteed never to change. Immutability is the secret behind Java’s very fast substring implementation.
Immutable objects are much better suited to be Hashtable keys. If you change the value of an object that is used as a hash table key without removing it and re-adding it you lose the mapping.
Since String is immutable, inside each String is a char[] exactly the correct length. Unlike a StringBuilder there is no need for padding to allow for growth.
If String were not final, you could create a subclass and have two strings that look alike when "seen as Strings", but that are actually different.
Asked by Khushi Kumari | Jun 16 2017 | Report this Report abuse
Replies (3)
View Roshan  shah 's Profile
Good you have correctly briefed the answer.
Jun 22 2017 | Report this Report abuse
View Tanya  Sharma 's Profile
String is immutuable because it is thread safe.
Jun 22 2017 | Report this Report abuse
View Diksha  Basu 's Profile
String is immutuable because it is thread safe.
Jun 21 2017 | Report this Report abuse