*
The moose likes Java in General and the fly likes String literal objects in String pool Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "String literal objects in String pool" Watch "String literal objects in String pool" New topic
Author

String literal objects in String pool

Bikash Paul
Ranch Hand

Joined: Dec 04, 2001
Posts: 342
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
Ajay Singh
Ranch Hand

Joined: Dec 13, 2006
Posts: 182
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

Joined: Dec 04, 2001
Posts: 342
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

Joined: Dec 13, 2006
Posts: 182
> 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

Joined: Dec 13, 2006
Posts: 182
concat creates a new string object (using StringBuffer), thats why the returned object is different.
Bikash Paul
Ranch Hand

Joined: Dec 04, 2001
Posts: 342
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

Joined: Dec 13, 2006
Posts: 182
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

Joined: Dec 04, 2001
Posts: 342
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

Joined: Dec 13, 2006
Posts: 182
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)
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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);


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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

Joined: Dec 13, 2006
Posts: 182
Right - but to achieve to a value "abc" it uses StringBuilder.
Ajay Singh
Ranch Hand

Joined: Dec 13, 2006
Posts: 182
Here is opcode for this code:



is



So, it clearly says that it uses StringBuilder to come to the result.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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

Joined: Dec 13, 2006
Posts: 182
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.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
Ajay Singh
Ranch Hand

Joined: Dec 13, 2006
Posts: 182
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

Joined: Dec 04, 2001
Posts: 342
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
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[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

Joined: Dec 04, 2001
Posts: 342
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
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Originally posted by Bikash Paul:
Is that right?


Yes.


[Jess in Action][AskingGoodQuestions]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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

Joined: Dec 04, 2001
Posts: 342
Hi All,

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

Thanks
Bikash
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String literal objects in String pool