Why String Class Is Made Immutable or Final in Java
Why String Class Is Made Immutable or Final in Java
1) String Pool
2) Security
3) Use of String in Class Loading Mechanism
4) Multithreading Benefits
5) Optimization and Performance
2. The "+" operator is overloaded for String in Java and usually use to concatenate two String.
Java doesn't support operator overloading but this is the special feature only available for
String class. also, "+" operator is internally implemented using either StringBuilder or
StringBuffer object and by using their append() method.
3. String class maintains a private pool of all String literals created in the JVM. Earlier this pool
was part of PermGen space but from JDK 8 the PermGen space has been removed and
String pool becomes a part of Java heap space.
4. Here are the steps to check if a given String s2 is the rotation of String s1 without
using String concatenation.
a. Check if the length of both Strings is same or not, If not then they are not
rotation. If yes, then proceed to next step.
b. Check if both Strings are equal, if yes then s2 is a rotation of s1. If not,
then move to next step.
c. Take the first string's first character and find the index in the second string.
If not found, then it's not the rotation, but if found, proceed to next step.
d. Subtract the length of the rotated string with the index found to find the
final position.
e. Check if the first character of the rotated String is same as the character at
the final position of input String and
the input.substring(finalPos) is equal to
the rotated.substring(0, index)
Now as soon as you make strong reference counter = null, counter object created
on line 1 becomes eligible for garbage collection; because it doesn't have any more
Strong reference and Weak reference by reference variable weakCounter can not
prevent Counter object from being garbage collected. On the other hand, had this
been Soft Reference, Counter object is not garbage collected until JVM absolutely
needs memory. Soft reference in Java is represented
using java.lang.ref.SoftReference class. You can use following code to create
a SoftReference in Java
prime = null; // now Counter object is eligible for garbage collection but
only be collected when JVM absolutely needs memory
After making strong reference null, Counter object created on line 2 only has one
soft reference which can not prevent it from being garbage collected but it can delay
collection, which is eager in case of WeakReference. Due to this major difference
between SoftReference and WeakReference, SoftReference are more suitable
for caches and WeakReference are more suitable for storing meta data. One
convenient example of WeakReference is WeakHashMap, which is another
implementation of Map interface like HashMap or TreeMap but with one unique
feature. WeakHashMap wraps keys as WeakReference which means once strong
reference to actual object removed, WeakReference present internally
on WeakHashMap doesn't prevent them from being Garbage collected.
digit = null;
1) Java Heap is divided into three generation for the sake of garbage collection. These are a
young generation, tenured or old generation, and Perm area.
2) New objects are created by young generation and subsequently moved to the old
generation.
3) String pool is created in PermGen area of Heap, garbage collection can occur in perm space
but depends upon JVM to JVM. By the way from JDK 1.7 update, String pool is moved to heap
area where objects are created.
4) Minor garbage collection is used to move an object from Eden space to survivor 1 and
survivor 2 space and major collection is used to move an object from young to tenured
generation.
5) Whenever Major garbage collection occurs application threads stop during that period
which will reduce application’s performance and throughput.
6) There are few performance improvements has been applied in garbage collection in java 6
and we usually use JRE 1.6.20 for running our application.
7) JVM command line options –Xmx and -Xms is used to setup starting and max size for Java
Heap. The ideal ratio of this parameter is either 1:1 or 1:1.5 based on my experience, for
example, you can have either both –Xmx and –Xms as 1GB or –Xms 1.2 GB and 1.8 GB.
8) There is no manual way of doing garbage collection in Java, but you can use various
reference classes e.g. WeakReference or SoftReference to assist garbage collector.