aspose file tools*
The moose likes Beginning Java and the fly likes String constant pool Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String constant pool" Watch "String constant pool" New topic
Author

String constant pool

Ritesh raushan
Ranch Hand

Joined: Aug 29, 2012
Posts: 100
why output is unequal.
class Pool
{
public static void main(String[] ar)
{
String s="abc";
String s1="ab";
s1=s1+"c";//why s1 is not created in pool this is also literal
if(s==s1)
System.out.println("equal");
else
System.out.println("unequal");
}
}
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Always use code tags:



Better now. Isn't it? When you say

, do you know what happens? Or let me put it this way, how many String objects are out there?


~ Mansukh
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7807
    
  21

Ritesh raushan wrote:s1=s1+"c";//why s1 is not created in pool this is also literal...

"c" might be a literal, but 's1 + "c"' is not; that's why.

And in fact "c" WILL be put into the pool as a result of that statement (at least, I'm pretty sure it will; you can test it).

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Winston Gutkowski wrote:
Ritesh raushan wrote:s1=s1+"c";//why s1 is not created in pool this is also literal...

"c" might be a literal, but 's1 + "c"' is not; that's why.

And in fact "c" WILL be put into the pool as a result of that statement (at least, I'm pretty sure it will; you can test it).

Winston


You gave it away Winston. I wanted to get it out of him. Anyways, its done now.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String constant pool