🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: cn.bbs.comp.lang.java
Article #19091

Re: һ�����String�Ƚ��ٶȵķ���

#19091
From: �ұ���������ѧ��18�����
Date: Sat, 10 Jan 2004 08:41
41 lines
2764 bytes
������ҵ���˼��:)
����,
String strA = new String("Hello");
String strB = new String("Hello");
��������Ǿ������ڵģ�����strA��new�ģ���strB�Ǵ��ļ��ж�����
strA==strBΪfalse
��Ƚ�strA��strBʱ������strA.equals(strB);
��sun��Ϊ��strA.intern()==strB.intern()��Ч��Ҫ��ǰ�߸ߡ�

intern��doc������ģ�
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. 

pool��ʼ�ǿյģ����strA��strB��ȵĻ������ص�ֵҲ����ȵġ��Ҿ���sun��������
֮������strA=strB.intern();��������Ϊ��ٶ�pool�е��Ǹ�reference����strA��

�� �� hijack (ǿ��?����Ǯô) �Ĵ������ᵽ: ��
: ���������
: String��
: ==��ʾ����Object�Ƿ���ͬһ��reference
: equals��ʾString�������Dz�����ͬ
: intern����ѯ��pool���Dz���������ƥ���String�����о�ʹ����û���ٴ�����
: eg:
: String str = new String("hi");
: String str2 = str.intern();
: ??
: >>  .................<����ʡ��>............


--
reoal

�� ��Դ:��Ʈ��ˮ�Ƽ� freecity.cnzju.net��[FROM: 10.111.155.229]
Technical Details
Message-ID: <4ACfgA$9yV@freecity.cnzju.net>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!elnk-pas-nf1!elnk-nf2-pas!newsfeed.earthlink.net!sjc70.webusenet.com!news.webusenet.com!maily.cic.tsinghua.edu.cn!news.ccie.net.cn!Freecity

Show Full Headers
Complete Article Headers
X-Google-Language: Chinese,GB
X-Google-Thread: f6cb0,5bd5248b5bc616b6
X-Google-Attributes: gidf6cb0,public
X-Google-ArrivalTime: 2004-01-10 00:45:24 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!elnk-pas-nf1!elnk-nf2-pas!newsfeed.earthlink.net!sjc70.webusenet.com!news.webusenet.com!maily.cic.tsinghua.edu.cn!news.ccie.net.cn!Freecity
From: reoal.bbs@freecity.cnzju.net (�ұ���������ѧ��18�����)
Newsgroups: cn.bbs.comp.lang.java
Subject: Re: һ�����String�Ƚ��ٶȵķ���
Date: 10 Jan 2004 08:41:08 GMT
Organization: Ʈ��ˮ�Ƽ�
Message-ID: <4ACfgA$9yV@freecity.cnzju.net>
Mime-Version: 1.0
Content-Type: text/plain; charset="gb2312"
Content-Transfer-Encoding: 8bit
X-Filename: Java/M.1073723626.A