Reputation: 35
I have one object of String as new String("abc");
How will i convert that object to String pool Object
Upvotes: 1
Views: 722
Reputation: 1020
Well, you don't convert a String to a StringPool. StringPool is a collection of strings managed by the JVM. You can, however, request a string to be included in the pool by requesting the VM to do so using intern() method of the String object if it has not been created as a literal (otherwise it is already there)
String test= "test".append("Case").append("String");
test.intern(); //string "testCaseString" will be interned
String check= "InternString";
check.intern(); //redundant as the string was already interned in the above creation statement
Upvotes: 0
Reputation: 40058
If your object is really new String("abc")
, then you should just use "abc"
instead of creating a new string and interning this one. "abc"
is interned anyway, as all string literals are.
I.e., the below boolean operation will be true
"abc" == new String("abc").intern()
Upvotes: 1
Reputation: 36304
String str= new String("abc").intern() // calling intern() will add the String object to the String pool.
Upvotes: 3