<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: String constant pool</title>
	<atom:link href="http://technote.wsjoung.com/2006/11/26/string-constant-pool/feed/" rel="self" type="application/rss+xml" />
	<link>http://technote.wsjoung.com/2006/11/26/string-constant-pool/</link>
	<description>simple note</description>
	<lastBuildDate>Fri, 05 Mar 2010 17:11:37 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Satya Pavan Kumar</title>
		<link>http://technote.wsjoung.com/2006/11/26/string-constant-pool/comment-page-1/#comment-137</link>
		<dc:creator>Satya Pavan Kumar</dc:creator>
		<pubDate>Fri, 05 Mar 2010 17:11:37 +0000</pubDate>
		<guid isPermaLink="false">http://wsjoung.wordpress.com/2006/11/26/string-constant-pool/#comment-137</guid>
		<description>thank u for providing this concept. i have doubt in this concept . i am writing two program differently 
program 1
class Test{
public static void main(String args[])
{
 int i;
String s1=&quot;Hello&quot;;
String s2=&quot;Hello&quot;;
String s3=new String(&quot;Hello&quot;);
String s4=new String(&quot;Hello&quot;);
i=s1.hashCode();
System.out.println(&quot;String s1-&gt;&quot;+i);  //365478
i=s2.hashCode();
System.out.println(&quot;String s2-&gt;&quot;+i); //365478
i=s3.hashCode();
System.out.println(&quot;String s3-&gt;&quot;+i); //365478
i=s4.hashCode();
System.out.println(&quot;String s4-&gt;&quot;+i); //365478
}
}

That comment is output. that means all hashcode are same .that mean entire content with respect heap and string constant pool are maintain single hashcode. If similar content is available w.r.t to heap as well as string constant pool it is pointed to single hashcode.

ok
 
observe this program

class Test{
public static void main(String args[])
{
String s1=&quot;hello&quot;;
String s2=&quot;hello&quot;;
String s3=new String(&quot;hello&quot;);
String s4=new String(&quot;hello&quot;);
System.out.println(&quot;w.r.t to heap &amp; string constant pool&quot;);
if(s1==s3)
System.out.println(&quot;String s1 is equal to s&quot;);
else
System.out.println(&quot;String s1 is not equals to s&quot;);  // this line executes 
System.out.println(&quot;w.r.t  string constant pool&quot;);
if(s2==s1)
System.out.println(&quot;String s1 is equal to s&quot;);  // this line executes 
else
System.out.println(&quot;String s1 is not equals to s&quot;);
System.out.println(&quot;w.r.t to heap &quot;);
if(s3==s4)
System.out.println(&quot;String s1 is equal to s&quot;);
else
System.out.println(&quot;String s1 is not equals to s&quot;);   // this line executes 
}
}

Observing this two program i got confused . plz clarify this doubt</description>
		<content:encoded><![CDATA[<p>thank u for providing this concept. i have doubt in this concept . i am writing two program differently<br />
program 1<br />
class Test{<br />
public static void main(String args[])<br />
{<br />
 int i;<br />
String s1=&#8221;Hello&#8221;;<br />
String s2=&#8221;Hello&#8221;;<br />
String s3=new String(&#8220;Hello&#8221;);<br />
String s4=new String(&#8220;Hello&#8221;);<br />
i=s1.hashCode();<br />
System.out.println(&#8220;String s1-&gt;&#8221;+i);  //365478<br />
i=s2.hashCode();<br />
System.out.println(&#8220;String s2-&gt;&#8221;+i); //365478<br />
i=s3.hashCode();<br />
System.out.println(&#8220;String s3-&gt;&#8221;+i); //365478<br />
i=s4.hashCode();<br />
System.out.println(&#8220;String s4-&gt;&#8221;+i); //365478<br />
}<br />
}</p>
<p>That comment is output. that means all hashcode are same .that mean entire content with respect heap and string constant pool are maintain single hashcode. If similar content is available w.r.t to heap as well as string constant pool it is pointed to single hashcode.</p>
<p>ok</p>
<p>observe this program</p>
<p>class Test{<br />
public static void main(String args[])<br />
{<br />
String s1=&#8221;hello&#8221;;<br />
String s2=&#8221;hello&#8221;;<br />
String s3=new String(&#8220;hello&#8221;);<br />
String s4=new String(&#8220;hello&#8221;);<br />
System.out.println(&#8220;w.r.t to heap &amp; string constant pool&#8221;);<br />
if(s1==s3)<br />
System.out.println(&#8220;String s1 is equal to s&#8221;);<br />
else<br />
System.out.println(&#8220;String s1 is not equals to s&#8221;);  // this line executes<br />
System.out.println(&#8220;w.r.t  string constant pool&#8221;);<br />
if(s2==s1)<br />
System.out.println(&#8220;String s1 is equal to s&#8221;);  // this line executes<br />
else<br />
System.out.println(&#8220;String s1 is not equals to s&#8221;);<br />
System.out.println(&#8220;w.r.t to heap &#8220;);<br />
if(s3==s4)<br />
System.out.println(&#8220;String s1 is equal to s&#8221;);<br />
else<br />
System.out.println(&#8220;String s1 is not equals to s&#8221;);   // this line executes<br />
}<br />
}</p>
<p>Observing this two program i got confused . plz clarify this doubt</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Naresh Hode</title>
		<link>http://technote.wsjoung.com/2006/11/26/string-constant-pool/comment-page-1/#comment-131</link>
		<dc:creator>Naresh Hode</dc:creator>
		<pubDate>Fri, 29 Jan 2010 11:24:07 +0000</pubDate>
		<guid isPermaLink="false">http://wsjoung.wordpress.com/2006/11/26/string-constant-pool/#comment-131</guid>
		<description>in this case also it is giving true</description>
		<content:encoded><![CDATA[<p>in this case also it is giving true</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Vawani sankar pati</title>
		<link>http://technote.wsjoung.com/2006/11/26/string-constant-pool/comment-page-1/#comment-43</link>
		<dc:creator>Vawani sankar pati</dc:creator>
		<pubDate>Tue, 11 Aug 2009 10:52:41 +0000</pubDate>
		<guid isPermaLink="false">http://wsjoung.wordpress.com/2006/11/26/string-constant-pool/#comment-43</guid>
		<description>hai  guy

look at  this  code some body  asked  abt  it

String s1 = “abc”;
String s2 = “ab”;
s2 = s2 + “c”;
if (s1 == s2)
System.out.println(”Equal”);
else
System.out.println(”false”);


ok  this  will show  false
because when u  are  trying to add  to  string  a  new  object  will  be  created so  the principle given on  the  top  will  work now, as it  will  create  a  object  in the heap and  the  heap  object  refrence  will be  given  back  so  the s1  and  s2  will  not  at  the  same  address,so  it will show  u  false  but  if  u  want  to  refer  to  the  &quot;abc&quot; in the  string  constant pool  then  you  can  use  the  &quot;intern()&quot; method  as  follows  so  both  will  refer  to  same  address
String s1=&quot;abc&quot;;
String s2=&quot;ab&quot;;
s2=s2+&quot;c&quot;;
s2=s2.intern();
if(s1==s2)
{
	System.out.println(&quot;true&quot;);
}
else
{
	System.out.println(&quot;false&quot;);

}


regards
vawani S  pati</description>
		<content:encoded><![CDATA[<p>hai  guy</p>
<p>look at  this  code some body  asked  abt  it</p>
<p>String s1 = “abc”;<br />
String s2 = “ab”;<br />
s2 = s2 + “c”;<br />
if (s1 == s2)<br />
System.out.println(”Equal”);<br />
else<br />
System.out.println(”false”);</p>
<p>ok  this  will show  false<br />
because when u  are  trying to add  to  string  a  new  object  will  be  created so  the principle given on  the  top  will  work now, as it  will  create  a  object  in the heap and  the  heap  object  refrence  will be  given  back  so  the s1  and  s2  will  not  at  the  same  address,so  it will show  u  false  but  if  u  want  to  refer  to  the  &#8220;abc&#8221; in the  string  constant pool  then  you  can  use  the  &#8220;intern()&#8221; method  as  follows  so  both  will  refer  to  same  address<br />
String s1=&#8221;abc&#8221;;<br />
String s2=&#8221;ab&#8221;;<br />
s2=s2+&#8221;c&#8221;;<br />
s2=s2.intern();<br />
if(s1==s2)<br />
{<br />
	System.out.println(&#8220;true&#8221;);<br />
}<br />
else<br />
{<br />
	System.out.println(&#8220;false&#8221;);</p>
<p>}</p>
<p>regards<br />
vawani S  pati</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: abhiroop</title>
		<link>http://technote.wsjoung.com/2006/11/26/string-constant-pool/comment-page-1/#comment-42</link>
		<dc:creator>abhiroop</dc:creator>
		<pubDate>Wed, 04 Feb 2009 17:26:19 +0000</pubDate>
		<guid isPermaLink="false">http://wsjoung.wordpress.com/2006/11/26/string-constant-pool/#comment-42</guid>
		<description>sorry
in the above  post read nikhil as  Niks..</description>
		<content:encoded><![CDATA[<p>sorry<br />
in the above  post read nikhil as  Niks..</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: abhiroop</title>
		<link>http://technote.wsjoung.com/2006/11/26/string-constant-pool/comment-page-1/#comment-41</link>
		<dc:creator>abhiroop</dc:creator>
		<pubDate>Wed, 04 Feb 2009 17:24:32 +0000</pubDate>
		<guid isPermaLink="false">http://wsjoung.wordpress.com/2006/11/26/string-constant-pool/#comment-41</guid>
		<description>@------Niks Says:
---------May 2, 2008 at 12:44 am
hi nikhil
String s2 = s2;
this line wont compiled
make it String s2 = s1;then evrything will work nd giv result
true
true</description>
		<content:encoded><![CDATA[<p>@&#8212;&#8212;Niks Says:<br />
&#8212;&#8212;&#8212;May 2, 2008 at 12:44 am<br />
hi nikhil<br />
String s2 = s2;<br />
this line wont compiled<br />
make it String s2 = s1;then evrything will work nd giv result<br />
true<br />
true</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: abhiroop</title>
		<link>http://technote.wsjoung.com/2006/11/26/string-constant-pool/comment-page-1/#comment-40</link>
		<dc:creator>abhiroop</dc:creator>
		<pubDate>Wed, 04 Feb 2009 17:17:54 +0000</pubDate>
		<guid isPermaLink="false">http://wsjoung.wordpress.com/2006/11/26/string-constant-pool/#comment-40</guid>
		<description>@  Munish Gogna Says:
     August 28, 2007 at 6:20 am
  hi manish.as u r taking strings inside double quotes so they become constants ..so s2 = s2 + “c”  this wont wrk..
or change the class .. make it StringBuilder there u can make modification in the string itself</description>
		<content:encoded><![CDATA[<p>@  Munish Gogna Says:<br />
     August 28, 2007 at 6:20 am<br />
  hi manish.as u r taking strings inside double quotes so they become constants ..so s2 = s2 + “c”  this wont wrk..<br />
or change the class .. make it StringBuilder there u can make modification in the string itself</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Niks</title>
		<link>http://technote.wsjoung.com/2006/11/26/string-constant-pool/comment-page-1/#comment-38</link>
		<dc:creator>Niks</dc:creator>
		<pubDate>Fri, 02 May 2008 05:44:10 +0000</pubDate>
		<guid isPermaLink="false">http://wsjoung.wordpress.com/2006/11/26/string-constant-pool/#comment-38</guid>
		<description>String s1=&quot;abs&quot;;
String s2 = s2;
System.out.println(s1==s2);

Output:
True

but

String s1=&quot;abs&quot;;
String s2 = s2;
s2=&quot;abs&quot;;
System.out.println(s1==s2);

Output:
false

note that here s2 and s1 point to same memory location till statment String s2=s1;

but when i assign s2=&quot;abs&quot;;
which has same value as s1 and right now its has same memoru location

then its pointing to different object?
why?

its means that while assign s2=&quot;abs&quot;.... it will make new object in constant pool???</description>
		<content:encoded><![CDATA[<p>String s1=&#8221;abs&#8221;;<br />
String s2 = s2;<br />
System.out.println(s1==s2);</p>
<p>Output:<br />
True</p>
<p>but</p>
<p>String s1=&#8221;abs&#8221;;<br />
String s2 = s2;<br />
s2=&#8221;abs&#8221;;<br />
System.out.println(s1==s2);</p>
<p>Output:<br />
false</p>
<p>note that here s2 and s1 point to same memory location till statment String s2=s1;</p>
<p>but when i assign s2=&#8221;abs&#8221;;<br />
which has same value as s1 and right now its has same memoru location</p>
<p>then its pointing to different object?<br />
why?</p>
<p>its means that while assign s2=&#8221;abs&#8221;&#8230;. it will make new object in constant pool???</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: srikanth</title>
		<link>http://technote.wsjoung.com/2006/11/26/string-constant-pool/comment-page-1/#comment-35</link>
		<dc:creator>srikanth</dc:creator>
		<pubDate>Mon, 29 Oct 2007 02:01:27 +0000</pubDate>
		<guid isPermaLink="false">http://wsjoung.wordpress.com/2006/11/26/string-constant-pool/#comment-35</guid>
		<description>s1=&quot;java&quot;
s2=&quot;&quot;;
s2=s1+s2;
if the above condition is checked it will return false. but how it is possible could any one tell me</description>
		<content:encoded><![CDATA[<p>s1=&#8221;java&#8221;<br />
s2=&#8221;";<br />
s2=s1+s2;<br />
if the above condition is checked it will return false. but how it is possible could any one tell me</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ram</title>
		<link>http://technote.wsjoung.com/2006/11/26/string-constant-pool/comment-page-1/#comment-34</link>
		<dc:creator>Ram</dc:creator>
		<pubDate>Mon, 08 Oct 2007 13:31:11 +0000</pubDate>
		<guid isPermaLink="false">http://wsjoung.wordpress.com/2006/11/26/string-constant-pool/#comment-34</guid>
		<description>it no more points to abc ? When did it point either ? s2 was pointing to &quot;ab&quot; but not &quot;abc&quot;</description>
		<content:encoded><![CDATA[<p>it no more points to abc ? When did it point either ? s2 was pointing to &#8220;ab&#8221; but not &#8220;abc&#8221;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: nittin gupta</title>
		<link>http://technote.wsjoung.com/2006/11/26/string-constant-pool/comment-page-1/#comment-36</link>
		<dc:creator>nittin gupta</dc:creator>
		<pubDate>Thu, 04 Oct 2007 10:50:48 +0000</pubDate>
		<guid isPermaLink="false">http://wsjoung.wordpress.com/2006/11/26/string-constant-pool/#comment-36</guid>
		<description>this above code prints not equal becoz
when we are giving statement s2=s2+&quot;c&quot;
prevoius s2 loose its reference ie it no more points to abc
a new object is created as abc and s2 point to it</description>
		<content:encoded><![CDATA[<p>this above code prints not equal becoz<br />
when we are giving statement s2=s2+&#8221;c&#8221;<br />
prevoius s2 loose its reference ie it no more points to abc<br />
a new object is created as abc and s2 point to it</p>
]]></content:encoded>
	</item>
</channel>
</rss>
