Article View: cn.bbs.comp.lang.java
Article #12450Re: ���ڴ�ֵ���Ǵ����õĽ�һ������
From: 98CS~��è~�����Կ�����
Date: Thu, 29 Jul 2004 01:30
Date: Thu, 29 Jul 2004 01:30
62 lines
4513 bytes
4513 bytes
��������ڴ�����ַ������������object��hashCode.
��Object��Ϊ��,toString()����ô�����:
public String toString()
{
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
�ٷ��ĵ�����hashCode()���������:
1.Whenever it is invoked on the same object more than once during an execu
tion of a Java application, the hashCode method must consistently return
the same integer, provided no information used in equals comparisons on
the object is modified. This integer need not remain consistent from on
e execution of an application to another execution of the same applicati
on.
2.If two objects are equal according to the equals(Object) method, then ca
lling the hashCode method on each of the two objects must produce the sa
me integer result.
3.It is not required that if two objects are unequal according to the equa
ls(java.lang.Object) method, then calling the hashCode method on each of
the two objects must produce distinct integer results. However, the pro
grammer should be aware that producing distinct integer results for uneq
ual objects may improve the performance of hashtables.
��Ҫ�ǵڶ���,���a.equals(b)Ϊ��,��ôa��b��hashCode�ͱ�����ͬ,�ص������
equals()�������, һ����Ȼ�ĵ�����, a.equals(b)�� a==b�����ǵȼ۵�,
��������������,һ���ܹ�����ǰ��;��֮��Ȼ.
����Object����˵, equals()��������:
public boolean equals(Object obj)
{
return this == obj;
}
���DZ����Ͳ���Ȼ��,�ٷ��ĵ���equalsΪ���Ҫ������ô����:
1.�Է�,���� x.equals(x) һ�������
2.����, ��� x.equals(y), y.equals(z)ͬʱΪ��������, x.equals(z)ҲΪ��
3.�Գ�. x.equals(y) ���Ϊ��, y.equals(x)Ҳһ��Ϊ��.
4. ��� x��y��ͬһ�����������,��ô x.equals(y) Ϊ��.
����Object��,������Ϊ�������ַ,�����������Ͳ�һ����.
ֻҪ��һ�� a.euals(b) Ϊ��, a==b Ϊ�ٵ����ӾͿ���ȷ��
hashCode����һ���ǵ�ַ��.
�� �� nue.bbs@bbs.sjtu.edu.cn (NUE) �Ĵ������ᵽ: ��
: System.out.print(object)
: �� �� onebird.bbs@bbs.nankai.edu.cn (�ټ���һ��) �Ĵ������ᵽ: ��
: : ����� ���������ڴ�����ַ��ʾ������
: : .................������ʡ�ԣ�
--
[1;37;40m [35m�X�T�T�T�j�T�T�T�T�j�T�T�j�T�T�j�T�T�T�T�T�T�j�T�T�T�T�T�T�[[37m
[1;37;40m [35m�U[32m����[35m�U�b�[�i�X�a�b�[�l�X�e�^�T�T�T�e�X�e�^�e�X�T�T�T�e�U[37m
[1;37;40m [35m�d�T�Y�X�a�X�p�a�U�b�[�^�j�a �b�j�T�e�X�p�[�X�T�g�V�T�T�T�g[37m [35m [37m [35m [37m [35m [37m [35m [37m
[1;37;40m [35m�U�b�T�a�X�a�^�e�m�T�m�e�U �b�T�m�T�T�a�d�a�^�e�^�[[33m�ҹ���[35m�U[37m
[1;37;40m [35m�^�T�T�T�m�T�T�T�T�T�T�T�m�T�T�T�T�T�T�T�m�T�T�T�T�m�T�T�T�a[37m
[1;37;40m [36m��Ϊ�㷭ɽԽ�룬ȴ���Ŀ��羰��[37m
[m[1;32m�� ��Դ:���Ұ��Ͽ�վ nkbbs.org��[FROM: 202.108.245.163][m
Technical Details
Message-ID:
<4DPVZb$9CJ@bbs.nankai.edu.cn>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!news.moat.net!ctu-peer!news.nctu.edu.tw!netnews.csie.nctu.edu.tw!maily.cic.tsinghua.edu.cn!news2.happynet.org!NanKaiShow Full Headers
Complete Article Headers
X-Google-Thread: f6cb0,d356ce699e5f20c7 X-Google-Attributes: gidf6cb0,public X-Google-Language: ENGLISH,ASCII Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!news.moat.net!ctu-peer!news.nctu.edu.tw!netnews.csie.nctu.edu.tw!maily.cic.tsinghua.edu.cn!news2.happynet.org!NanKai From: xeon.bbs@bbs.nankai.edu.cn (98CS~��è~�����Կ�����) Newsgroups: cn.bbs.comp.lang.java Subject: Re: ���ڴ�ֵ���Ǵ����õĽ�һ������ Date: 29 Jul 2004 01:30:50 GMT Organization: �Ұ��Ͽ�վ Message-ID: <4DPVZb$9CJ@bbs.nankai.edu.cn> X-Filename: Java/M.1091063221.A