Java Interview Series
Java INTERVIEW
( PART- 7 )
Developer_pani
Hi Preethi..! I am Pani.
Hey Pani ! Thanks
I am going to take
for giving me this
your technical round.
oppportunity.
I am going to ask
few Java Questions.
okay,
pani.
A String is made
immutable due to the
Apart from the security
following reasons:
aspect, what are the reasons
behind making strings 1. String pool
immutable in Java? 2. Multithreading
3. Collections
1. String Pool
Designers of Java were aware of the fact that
String data type is going to be majorly used by
the programmers and developers. Thus, they
wanted optimization from the beginning. They
came up with the notion of using the String
pool (a storage area in Java heap) to store the
String literals.
They intended to decrease the temporary
String object with the help of sharing. An
immutable class is needed to facilitate sharing.
The sharing of the mutable structures between
two unknown parties is not possible. Thus,
immutable Java String helps in executing the
concept of String Pool.
2. Multithreading
The safety of threads regarding the String
objects is an important aspect in Java. No
external synchronization is required if the
String objects are immutable.
Thus, a cleaner code can be written for
sharing the String objects across different
threads. The complex process of concurrency
is facilitated by this method.
3. Collections
In the case of Hashtables and HashMaps, keys
are String objects. If the String objects are not
immutable, then it can get modified during the
period when it resides in the HashMaps.
Consequently, the retrieval of the desired data
is not possible. Such changing states pose a lot
of risks. Therefore, it is quite safe to make the
string immutable
To be continued in
next part...
Developer_pani
Join the telegram channel te get the notes,
Interview Q & A and other resources
( Link in Bio )