« | July 2025 | » | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | | | |
| 公告 |
关注电子政务、大型企业应用开发、Web、Workflow、MOM、MDA、RCP、GEF email:gmluyang@gmail.com
|
Blog信息 |
blog名称:SixSun的Blog 日志总数:152 评论数量:372 留言数量:13 访问次数:2372842 建立时间:2004年12月13日 |

| |
[J2SE / 基础类]String( Equal , ==, intern()) 随笔, 心得体会, 软件技术
SixSun 发表于 2006/3/1 9:48:11 |
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'>
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()); }
} |
|
|