Bookmark Topic Watch Topic
  • New Topic

How many objects will be created in the string pool?

 
Kaushik Baral
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 247
  • Mark post as helpful
  • send pies
  • Report post to moderator
2 Objects will be created.
 
Kaushik Baral
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Report post to moderator
could you please explain how two objects will be created... thanks in advance
 
Rameshwar Soni
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20613
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator
Rameshwar Soni wrote:2 Objects will be created.

But only one in the String pool.
 
Rameshwar Soni
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Report post to moderator
thanks everybody for your time. thing is clear now.
 
Nam Ha
Ranch Hand
Posts: 50
Java
  • Mark post as helpful
  • send pies
  • Report post to moderator
Thanks Rameshwar Soni for your great explanation!
I got to understand String pool better now
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49865
71
  • Mark post as helpful
  • send pies
  • Report post to moderator
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?
 
    Bookmark Topic Watch Topic
  • New Topic