How many objects will be created in the string pool?
Kaushik Baral
Greenhorn
Joined: Aug 08, 2009
Posts: 18
posted
0
If I have a code like this
String s1 = new String ("abc");
String s2 = "abc";
String s3 = "abc";
then how many objects will be created in the string pool?
Rameshwar Soni
Ranch Hand
Joined: Feb 03, 2011
Posts: 246
posted
0
2 Objects will be created.
Kaushik Baral
Greenhorn
Joined: Aug 08, 2009
Posts: 18
posted
0
could you please explain how two objects will be created... thanks in advance
Rameshwar Soni
Ranch Hand
Joined: Feb 03, 2011
Posts: 246
posted
0
There are 2 ways to create String Objects in Java
(1) Using the new operator i.e.
(2) Using the String Literal i.e.
.
.
Now String allocation is costly in both time and memory so the JVM(Java Virtual Machine) performs some tasks? WHAT TASKS?
See whenever you are using the "new" operator the object is created, JVM will not look in string pool it is just going to create the Object, but when you are
using the string literals for creating String objects then JVM will perform the task of looking in the string pool i.e.
When you will write the JVM will look in string pool and check if "abc"already exists or not. If it exists then then a reference is returned
to the already existed string "abc" and new object is not created and if it doesn't exists then a object is created.
So in your case
(a)--------Since "new" is used to therefore Object is created
(b)----using String literal a object is created and "abc" is not in string pool therefore Object is created
(c)----Again using String literal and "abc" is in string pool therefore Object is not created.
.
.
You can also check it out by using the following the code
Hope this helps...Note == is used to see if Object are equal and equals(Object) method is used to see if content are equal.
Yes its my mistake while replying to the question i had kept only "HOW MANY OBJECTS WILL BE CREATED" in my mind so i replied 2 Objects and the question was "HOW MANY OBJECTS IN STRING POOL" so it is ONE as Rob said and total 2 Objects.
Kaushik Baral
Greenhorn
Joined: Aug 08, 2009
Posts: 18
posted
0
thanks everybody for your time. thing is clear now.
Rameshwar Soni wrote:
(a)--------Since "new" is used to therefore Object is created
(b)----using String literal a object is created and "abc" is not in string pool therefore Object is created
String s2="abc"; won't create any object in literal pool because first statement String s1=new String("abc") creates two objects first on heap second on pool, so because of there is already "abc" literal is provided so second statement wont create any object again,s2 will just refer the second object in pool.
Tell the difficulties that i am difficult.
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32675
4
posted
0
saloni jhanwar wrote: . . .
String s2="abc"; won't create any object in literal pool because first statement String s1=new String("abc") creates two objects first on heap second on pool, so because of there is already "abc" literal is provided so second statement wont create any object again,s2 will just refer the second object in pool.
“second object in pool” is confusing. Do you mean that the second variable will refer to the same object in the String pool?