Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String constant pool

 
Ritesh raushan
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Winston Gutkowski
Bartender
Pie
Posts: 10501
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic