• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

String is created on pool?

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


According to my understanding, the line 1 creates a string literal "Hello" in the string pool. However, this one is not used for reference s: another string object is created in the heap.
Now, the string "Hello" created in the pool, does not have a reference.
So in the lines 2 and 3,is the same string "Hello" used from the pool used or is a new string created in the pool?
At the end of these lines of code, how many objects are in the pool?

I feel the first "Hello" in pool cannot be used later. Pls confirm.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ONLY 2 OBJECTS are created..
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"sunny .....",
Welcome to the ranch. You may not be aware of the ranch Naming Policy. Please read it carefully and change your name accordingly (you need to set both first and last names, ..... is not valid). Thank you.
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Two objects are created. One on the pool which is referred by s1 and s2, another is created on heap referred by s.
 
Vidhya Ramaswamy
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This code surely creates 2 objects, one in the string pool (literal "Hello") and another String object in the heap referenced by s.

Now, my question is: What happens to the "Hello" created in the pool?
Is the same one used for the other string references too? (s1 and s2)??
I don't think so, wanted to confirm.
I think there are 2 string objects in the pool and 1 in the heap.
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Now, my question is: What happens to the "Hello" created in the pool?
Is the same one used for the other string references too? (s1 and s2)??
I don't think so, wanted to confirm.
I think there are 2 string objects in the pool and 1 in the heap.



Whenever a string literal is used, it first checks if same string literal exists in the pool. If it exists then reference of it is assigned or else it creates a new string, puts it in pool and then assigns the reference.



In this case at lines 2 and 3, s1 and s2 refer to same Hello object which was created in a pool at line 1.

Hence only 2 objects are created in this scenario.

Murali...
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In This Case two Object will Create .

But How the pooling Mechanism work on different Object i.e

let
String str=new String("Hello");
String str1="Hello";
String str2="Heloo";
String str3="World";

In the Above Case it will create also two Object.But How the pooling
concept will work?

Cheers
Khirod Patra
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys -

Just so you know - while the String pool can be an important concept, it's NOT on the exam. So if you're focused on studying for the exam, you can skip anything to do with the String pool.
 
Vidhya Ramaswamy
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Bert,
Thanks for replying to the post and letting us know that String pool is not on te exam.

However, can you pls clarify the doubt-what happens to the string in the pool which doesn't have a reference as in #1?
 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

let
String str=new String("Hello");//line1
String str1="Hello";//line2
String str2="Heloo";//line3
String str3="World";//line4

In the Above Case it will create also two Object.But How the pooling
concept will work?



I think in the above case in line1 2 Hello objects are created one on heap and one in pool. In line 2 same object in pool is assigned to str1. In line 3 another object(Heloo) is created in the pool and assigned to str2 and in line 4 World object is created in the pool and assigned to str3. So a total of 4 objects are created, one on the heap and 3 in pool. Please correct me if I'm wrong. Thanks.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic