aspose file tools*
The moose likes Beginning Java and the fly likes How many objects will be created in the string pool? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic
Author

How many objects will be created in the string pool?

Kaushik Baral
Greenhorn

Joined: Aug 08, 2009
Posts: 23
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: 247
2 Objects will be created.
Kaushik Baral
Greenhorn

Joined: Aug 08, 2009
Posts: 23
could you please explain how two objects will be created... thanks in advance
Rameshwar Soni
Ranch Hand

Joined: Feb 03, 2011
Posts: 247
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

Joined: Oct 27, 2005
Posts: 19762
    
  20

Rameshwar Soni wrote:2 Objects will be created.

But only one in the String pool.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rameshwar Soni
Ranch Hand

Joined: Feb 03, 2011
Posts: 247
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: 23
thanks everybody for your time. thing is clear now.
Nam Ha
Ranch Hand

Joined: Aug 05, 2011
Posts: 50

Thanks Rameshwar Soni for your great explanation!
I got to understand String pool better now


Java Object Creation
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

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: 39869
    
  28
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?
 
Don't get me started about those stupid light bulbs.
 
subject: How many objects will be created in the string pool?