Object.equals()


终于弄明白下面这段程序的意思了:
class Value {
int i;
}
public class EqualsMethod {
public static void main(String[] args) {
Value v1 = new Value();
Value v2 = new Value();
v1.i = v2.i = 100;
System.out.println(v1.equals(v2));
}
}
输出结果:false
Object类里的equals方法比较的是两个对象的引用是否指向相同的对象,在String类中,覆盖了Object类的equals方法,比较的是两个对象的值。

——2006/3/21

Advertisements

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s