Why special preferene is given to Strings?
I mean why string objects are not directly created inside heap memory?
Why they are been created inside intern pool which is again a subset of heap memory?
Doubt regarding Strings
Let say you have these two strings
String one = “abc”;
String two = new String(“abc”);
The first one is called as a String Literal and created at the time of compilation of the program and the 2nd one is string object and is created at the runtime.
As you used new keyword in 2nd case so it is allocated in heap.
In the first case the objects are created with the mechanism called interning. When you try to create another string literal representing the same sequence of characters, then instead of creating a new object compiler will refer to the previous string created and stored in the string pool
So , you mean that whenever we create string by using new keyword only then that string object will be created inside heap . Otherwise in the remaining cases , it will be created inside internal pool .