  | Mailing List | | Home | | Forum Home | | JBoss - Java Application Server | | Tomcat - JSP/Servlet container | | Struts - A MVC web framework | | iText - An open source PDF Java Library | | JDOM - JDOM XML Parser | | JSP - A mailing list about Java Server Pages specification and reference | | J2EE - A mailing list for Java(tm) 2 Platform, Enterprise Edition | | J2EE Pattern - An interest list for Sun Java Center J2EE Pattern Catalog | | Servlet - A mailing list for discussion about Sun Microsystem's Java Servlet API Technology | |
Struts & Hibernate
|
|
|
  | | | Intern | Intern 2005-10-18 - By Bharath Ganesh
Back Hi
Ya, Java has internal pool of literals.
When u say: String s1="India" a string object is created and its ref is put in internal pool. So next time when u create a new ref for the same string "India", the new ref will also refer to the same object. U can also achieve this by saying String s1=new String("Hello"); String s2=s1.intern(); String s3="Hello";
s2==s3 // Will be true s1==s2 //False
And u dont get into any risk by sharing the instance cos Strings are immutable.
P.S. Look into intern() in String api.
===== cheerio bharath I Blog at http://jroller.com/page/bharath -- -- Original Message -- -- From: Sonali Kansara To: J2EE-INTEREST@(protected) Sent: Monday, October 17, 2005 6:33 PM Subject: Re: new String
Yes Frans is right ......... its a concept called interned strings .......... .
In case of compile time constant strings , a string available from the pool is utilized instead of creating a new instance of the string .
So
String s1 = "Test5" ; String s2 = "Test5" ; String s3 = "Test"+"5" ;
int i = 5 ; String s4 = "Test"+i ;
Here s1 , s2 , s3 are refering to the same instance of Test5 while s4 creates a new instance .
Regards , Sonali Notice: The information contained in this e-mail message and/or attachments to it may contain confidential or privileged information. If you are not the intended recipient, any dissemination, use, review, distribution, printing or copying of the information contained in this e-mail message and/or attachments to it are strictly prohibited. If you have received this communication in error, please notify us by reply e-mail or telephone and immediately and permanently delete the message and any attachments. Thank you =========================================================================== To unsubscribe, send email to listserv@(protected) and include in the body of the message "signoff J2EE-INTEREST". For general help, send email to listserv @(protected) and include in the body of the message "help".
=========================================================================== To unsubscribe, send email to listserv@(protected) and include in the body of the message "signoff J2EE-INTEREST". For general help, send email to listserv@(protected) and include in the body of the message "help".
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML><HEAD> <META http-equiv=Content-Type content="text/html; charset=iso-8859 (See http://iso-8859.ora-code.com)-1"> <META content="MSHTML 6.00.2800.1522" name=GENERATOR> <STYLE></STYLE> </HEAD> <BODY bgColor=#ffffff> <DIV><FONT face=Verdana color=#000080 size=2> <DIV><FONT face=Verdana color=#000080 size=2>Hi</FONT></DIV> <DIV><FONT face=Verdana color=#000080 size=2></FONT> </DIV> <DIV><FONT face=Verdana color=#000080 size=2>Ya, Java has internal pool of literals.</FONT></DIV> <DIV><FONT face=Verdana color=#000080 size=2></FONT> </DIV> <DIV><FONT face=Verdana color=#000080 size=2>When u say:</FONT></DIV> <DIV><FONT face=Verdana color=#000080 size=2>String s1="India"</FONT></DIV> <DIV><FONT face=Verdana color=#000080 size=2>a string object is created and its ref is put in internal pool. </FONT></DIV> <DIV><FONT face=Verdana color=#000080 size=2>So next time when u create a new ref for the same string "India", the new ref will also refer to the same object . </FONT></DIV> <DIV><FONT face=Verdana color=#000080 size=2>U can also achieve this by saying </FONT></DIV> <DIV><FONT face=Verdana color=#000080 size=2>String s1=new String("Hello");<BR>String s2=s1.intern();<BR>String s3="Hello";</FONT></DIV><FONT face=Verdana color=#000080 size=2> <DIV><BR>s2==s3 // Will be true</DIV> <DIV>s1==s2 //False</FONT></DIV> <DIV><FONT face=Verdana color=#000080 size=2></FONT> </DIV> <DIV><FONT face=Verdana color=#000080 size=2>And u dont get into any risk by sharing the instance cos Strings are immutable.</FONT></DIV> <DIV><FONT face=Verdana color=#000080 size=2></FONT> </DIV> <DIV><FONT face=Verdana color=#000080 size=2>P.S. Look into intern() in String api.</FONT></DIV> <DIV><FONT face=Verdana color=#000080 size=2></FONT><FONT face=Verdana color=#000080 size=2></FONT><FONT face=Verdana color=#000080 size=2></FONT> <FONT face=Verdana color=#000080 size=2></FONT><BR><FONT face=verdana color=navy size=2>=====<BR>cheerio <BR>bharath</FONT></DIV> <DIV><FONT face=verdana color=navy size=2>I Blog at http://jroller.com/page/bharath </FONT></DIV> <BLOCKQUOTE dir=ltr style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000080 2px solid; MARGIN-RIGHT: 0px"> <DIV style="FONT: 10pt arial">-- -- Original Message -- -- </DIV> <DIV style="BACKGROUND: #e4e4e4; FONT: 10pt arial; font-color: black"><B>From:</B> <A title=Sonali.Kansara@(protected) href="mailto:Sonali.Kansara@(protected)">Sonali Kansara</A> </DIV> <DIV style="FONT: 10pt arial"><B>To:</B> <A title=J2EE-INTEREST@(protected) href="mailto:J2EE-INTEREST@(protected)">J2EE-INTEREST@(protected)</A> </DIV> <DIV style="FONT: 10pt arial"><B>Sent:</B> Monday, October 17, 2005 6:33 PM</DIV> <DIV style="FONT: 10pt arial"><B>Subject:</B> Re: new String</DIV> <DIV><BR></DIV><BR><FONT face=sans-serif size=2>Yes Frans is right ......... its a concept called interned strings ...........</FONT> <BR><BR><FONT face=sans-serif size=2>In case of compile time constant strings , a string available from the pool is utilized instead of creating a new instance of the string .</FONT> <BR><BR><FONT face=sans-serif size=2>So </FONT><BR><BR><FONT face=sans-serif size=2>String s1 = "Test5" ;</FONT> <BR><FONT face=sans-serif size=2>String s2 = "Test5" ; </FONT><BR><FONT face=sans-serif size=2>String s3 = "Test"+"5" ; </FONT><BR><BR><FONT face=sans-serif size=2>int i = 5 ;</FONT> <BR><FONT face=sans-serif size=2>String s4 = "Test"+i ;</FONT> <BR><BR><FONT face=sans-serif size=2>Here s1 , s2 , s3 are refering to the same instance of Test5 while s4 creates a new instance .</FONT> <BR><BR><BR><FONT face=sans-serif size=2>Regards , </FONT><BR><FONT face=sans-serif size=2>Sonali </FONT><BR> <TABLE> <TBODY> <TR> <TD bgColor=#ffffff><FONT color=#000000>Notice: The information contained in this e-mail message and/or attachments to it may contain confidential or privileged information. If you are not the intended recipient, any dissemination, use, review, distribution, printing or copying of the information contained in this e-mail message and/or attachments to it are strictly prohibited. If you have received this communication in error, please notify us by reply e-mail or telephone and immediately and permanently delete the message and any attachments. Thank you<BR></FONT></TD></TR></TBODY></TABLE>===================================== ====================================== To unsubscribe, send email to listserv@(protected) and include in the body of the message "signoff J2EE-INTEREST". For general help, send email to listserv@(protected) and include in the body of the message "help". <P></P></BLOCKQUOTE></FONT></DIV></BODY></HTML> =========================================================================== To unsubscribe, send email to listserv@(protected) and include in the body of the message "signoff J2EE-INTEREST". For general help, send email to listserv@(protected) and include in the body of the message "help". <p>
|
|
 |