阿布的天空,只做收集...                                                                                                    Manage    NewBlog    Exit


«December 2025»
123456
78910111213
14151617181920
21222324252627
28293031


公告

Welcome to iPUD's Sky!


我的分类(专题)

日志更新

最新评论

留言板

链接

Blog信息
blog名称:阿布的天空
日志总数:29
评论数量:39
留言数量:-32
访问次数:167357
建立时间:2006年3月21日




[JAVA学习]String( Equal , ==, intern())
文章收藏,  网上资源,  软件技术

阿布 发表于 2006/4/2 14:16:31

String java.lang.String.intern()Returns a canonical representation for the string object. A pool of strings, initially empty, is maintained privately by the class String. When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned. It follows that for any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true. All literal strings and string-valued constant expressions are interned. String literals are defined in §3.10.5 of the Java Language Specification Returns:a string that has the same contents as this string, but is guaranteed to be from a pool of unique strings. Test Game:500)this.width=500'>500)this.width=500" border=0> package org.sixsun.designpattern.flyweight; import junit.framework.TestCase; public class TestFlyweight extends TestCase {  String str1;          String str2;          String str3;         String str4;  public static void main(String[] args) { }  public TestFlyweight(String name) {  super(name); }  protected void setUp() throws Exception {  super.setUp();  str1 = "fly";           str2 = "weight";           str3 = "flyweight";               str4 = str1 + str2;  }  protected void tearDown() throws Exception {  super.tearDown(); }  public void testEqual1() {  assertFalse(str3==str4); }  public void testEqual2() {  assertTrue(str3.equals(str4)); }   public void testEqual3() {  assertFalse( str3 == "weight" + "flyweight" ); }  public void testEqual4() {  assertFalse( str3 == (new String("weight") + new String ("flyweight")) ); }   public void testEqual5() {  assertFalse( str3 == new String ("weight" + "flyweight") ); }  public void testEqual6() {  assertFalse( str3.equals("weight" + "flyweight") ); }   public void testEqual7() {  assertFalse( str3 == "weightfly" ); }   public void testEqual8() {  assertFalse( str3 == new String("weightfly") ); }     public void testEqual9() {  assertTrue( str1=="fly" ); }  public void testEqual10() {  assertFalse( str1==new String("fly") ); }  public void testEqual11() {  assertTrue( str1.equals("fly") ); }   public void testEqual12() {  assertFalse(str3==(str1 + str2)); }  public void testEqual13() {  assertFalse(str4==(str1 + str2)); }  public void testEqual14() {  assertFalse((str1 + str2)==(str1 + str2)); }  public void testEqual15() {  assertTrue((str1 + str2).equals(str1 + str2)); }   public void testEqualIntern() {  assertTrue(str3==(str1 + str2).intern()); }  }  


阅读全文(1887) | 回复(0) | 编辑 | 精华
 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.547 second(s), page refreshed 144802825 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号