• Post Reply Bookmark Topic Watch Topic
  • New Topic

How many Objects created with new String("")  RSS feed

 
Ranch Hand
Posts: 44
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am little confused about String creation in java.
Doubt 1: How String objects assigned to Pool area:
1. String s="in pool";
2. String s1= new String("not in pool");

How many objects created in statement 1 and 2. According to recent discussion with my colleague, one object created in String pool in case 1. And in case 2, two objects are created, one as literal goes to String pool and other with new() opr goes to Heap.

If above is correct, Ain't we wasting double memory for same object ? Really need clear understanding on this

Doubt 2: How does intern() work: Please see if my below explanation is correct

1. If String literal is already present in String pool , and i create a same string with new operator, reference to object is changed to pool area.
2. If String object is created using new operator and intern is called on it. If same string object is not present in the String Pool, Its moved to String pool and reference to this in Pool is returned.
 
Bartender
Posts: 543
4
Java Netbeans IDE Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you test it and be 100% sure?
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Surinder Mehra wrote:I am little confused about String creation in java.
Doubt 1: How String objects assigned to Pool area:
1. String s="in pool";
2. String s1= new String("not in pool");

How many objects created in statement 1 and 2. According to recent discussion with my colleague, one object created in String pool in case 1. And in case 2, two objects are created, one as literal goes to String pool and other with new() opr goes to Heap.

Your colleague is correct.

Surinder Mehra wrote:If above is correct, Ain't we wasting double memory for same object ? Really need clear understanding on this

Yes. That is why it's very rare that you will see the second example used. 99.99% of the time (if not more) you should use the first example.

Surinder Mehra wrote:Doubt 2: How does intern() work: Please see if my below explanation is correct

1. If String literal is already present in String pool , and i create a same string with new operator, reference to object is changed to pool area.

No. If you use new a new String instance is always created, as your colleague explained to you.

Surinder Mehra wrote:
2. If String object is created using new operator and intern is called on it. If same string object is not present in the String Pool, Its moved to String pool and reference to this in Pool is returned.

The answer to this is in the String javadoc.
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a very common question, presumably because it appears in an SCJP book, often seen in another forum, whither I shall move this thread. Once you have passed that exam, you never need to know the answer to that question again.
Please search: particularly for the old JavaRanch journal article called “Strings, literally” by Corey McGlone, which will probably answer all those questions.
 
Surinder Mehra
Ranch Hand
Posts: 44
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Everyone.... Its clear now.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!