• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String literal objects in String pool

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



I couldn't understand why it's printing only "In Equals method".

"As per java API if compiler encounters a String literal , it checks the pool to see if an identical String already exists. If a match found the reference to the new literal is directed to the existing String."

when I'm concating "c" with str1 then new String formed with value "abc" but identical String already exist in string pool then why str and str1 is not refering to same String object in pool?

Thanks
Bikash
 
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it checks in the pool only if the string is constructed as "xya", (meaning not using constructor). So,


Here string a and b are same but not c.
However you can invoke intern() on the c to get the pooled instance.
So,
String d = c.intern();

Now string d is same as a, b.
 
Bikash Paul
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ajay,

Thanks for your reply but I'm not using new String() I'm just modifying the value of String literal str1.

When we say String str1 = new String("abc") then java will create a new String object in normal memory(non-pool) and str1 will refer to it and in addition literal "abc" will be placed in the pool if match is not found.

Thanks
Bikash
 
Ajay Singh
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> in addition literal "abc" will be placed in the pool if match is not found.
No. When you create a string invoking constructor, it doesn't go to pool. It would create a pool instance if you call "intern"
method on the instance and return it back.
 
Ajay Singh
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
concat creates a new string object (using StringBuffer), thats why the returned object is different.
 
Bikash Paul
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ajay,

I'm not agree with you.

String a ="abc"; // creates one String object and one reference variable

String a = new String("abc") //creates two String objects and one reference variable.

Thanks
Bikash
 
Ajay Singh
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let me try understand your view.
So, when i create a string by calling constructor 2 string objects are created one on pool area one in non-pool area? Is this
correct?
 
Bikash Paul
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ajay,

Yes.

But I couldn't understand how concat() method is related to StringBuffer?

My guess is when we are concating may be compiler creating String object in normal memory(non-pool).

Thanks
Bikash
 
Ajay Singh
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bikash,

1. String a = new String("abc");
It just invokes a constructor of string that takes another
string. So, if you mean 2 objects as
a. one corresponding to "abc"
b. another string that is created from constructor.
its correct.

2. concat() - I didn't mean concat function of string. I meant
concatenation like this:
String a = "ab" + "c";

this internally uses StringBuffer (or StringBuilder for newer JVMs)
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bikash Paul:

My guess is when we are concating may be compiler creating String object in normal memory(non-pool).



When you are calling the concat method, the compiler won't do any concatenation, it just will generate the byte code for the method call. The actual concatenation than happens at runtime. And the last line in String.concat is

return new String(0, count + otherLen, buf);
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ajay S:

String a = "ab" + "c";

this internally uses StringBuffer (or StringBuilder for newer JVMs)



Not correct. The compiler is intelligent enough to see that "ab" + "c" is a "constant expression" and calculates it value at compile time. Therefore in this case the String "abc" would actually be put into the constant pool!
 
Ajay Singh
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right - but to achieve to a value "abc" it uses StringBuilder.
 
Ajay Singh
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is opcode for this code:



is



So, it clearly says that it uses StringBuilder to come to the result.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, but that wasn't your original example. Your original example had a constant expression, your new example hasn't.

Try



in contrast.
 
Ajay Singh
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was try to anwer this


Not correct. The compiler is intelligent enough to see that "ab" + "c" is a "constant expression" and calculates it value at compile
time. Therefore in this case the String "abc" would actually be put into the constant pool!



this
String x = "ab" + "c";
doesn't keep x on the pool.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But Ajay, you keep changing your example. Your "Bob DoleDole" example won't appear in the string pool, but "abc" will - because when you write "ab" + "c" together on one line, without using any variables in the expression - it becomes a constant expression. So "abc" is kept in the string pool. Therefore this statement:

this
String x = "ab" + "c";
doesn't keep x on the pool.


is FALSE.
 
Ajay Singh
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jim,
You are right I was showing different example. String x = "ab" + "c"; keeps the generated string on the pool. Sorry about
misunderstanding.
 
Bikash Paul
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,




But still I didn't get my answer if we all agree on that new String "abc" will keep in String constant pool but identical String "abc" already exist in pool then why str1 is not refering to same String object which is already refering by str.

if(str == str1){
System.out.println("In == method"); // returning false
}

Thanks
Bikash

 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Bikash Paul]: if we all agree on that new String "abc" will keep in String constant pool

But we don't agree to that point. The new "abc", the one created by concat(), is not in the string pool.
 
Bikash Paul
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jim,

That means when we use str1 = str1.concat("c") it's cretaing String object in normal memory(non-pool) thats why it's returning false. Is that right?

Thanks
Bikash
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bikash Paul:
Is that right?



Yes.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bikash Paul:
Jim,

That means when we use str1 = str1.concat("c") it's cretaing String object in normal memory(non-pool) thats why it's returning false. Is that right?

Thanks
Bikash



Yes, because that concatenation is not a constant expression, and therefore not performed by the compiler, but at runtime by the JVM. To quote myself:


When you are calling the concat method, the compiler won't do any concatenation, it just will generate the byte code for the method call. The actual concatenation than happens at runtime. And the last line in String.concat is

return new String(0, count + otherLen, buf);

 
Bikash Paul
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

Thanks for your reply.Now it's clear to me.

Thanks
Bikash
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic