I\'ve been studying Java String for a while. The following questions are based on the below posts
Java String is special
Immutability of String in java
Immutability is not a feature of new
, it is a feature of the class String
. It has no mutator methods, so it immutable.
Strings created will always be immutable regardless of how they are created.
Answers to your questions:
The only difference is:
When string is created like -- {String a = "Hello World!";}
then only one object gets created.
And when it is created like -- {String b = new String("Hello World!");}
then two objects get created. The first one, because you have used 'new' keyword and the second one because of the String property.
Yes, for sure. There will be an empty entry created in the pool.
1-String is immutable.See this:
Is a Java string really immutable?
So you can create it with many way.
2-Short answer: Yes will be empty.