第一个不用说了把?第二个你看看,那是String么?明明是对象的地址嘛,怎么可能一样啊?f1.num 和 f2.num 这才是String,你equals 下看看是不是一样。看java的API,java.lang.Object.equals(Object obj)这样描述:The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any reference values x and y, this method returns true if and only if x and y refer to the same object (x==y has the value true). 只有 f1 和 f2 指向同一个对象的时候才比较得 true,也就是地址相等。而不是什么对象的结构内容。至于String的比较,f1和f2地址是不同的,不过这里比较的却是内容,为什么是这样,我也不太清楚,可能要问SUN是怎么发明的了。不过对于普通的一个类的实例,确实是比较引用的地址的,也就是是不是指向同一个咚咚。也许,我猜测,equals是不是比较引用指向的内容,对于String,引用的地址不一样,可是指向的内容的地址却一样?(因为常量池的缘故?)对于一般的object,引用指向的内容的地址就不一样了,因为那可不是什么String,没什么常量池了。关于常量池。可以看String.intern()的API说明: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.
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.
解决方案 »
- 请问如何用java将文件读入一个字符串,并把文件中每一行的换行换成空格,从而形成一个连续的字符串
- System.currentTimeMillis()获取的时间怪怪的
- 请教疑惑
- 简易秒表,线程有问题,请高手相助
- 哪里可以下载lomboz?
- 诡异的数组越界,大家帮我看一下吧,一定给分!!!
- setHeader()/addHeader()怎么不起作用?
- 请教:写好了某个application并测试通过以后,接下来应该如何做?
- 谁用JAVAMAIL做过邮件发送程序?如何处理各种类型的附件?
- 求大神,用JAVA绘制正弦函数波形,向x轴右侧不断移动
- 高分请教:如何将一个对象完全克隆到另外一个新对象里面呢?
- "java开源"到底是指开放什么东西?jdk的源码不是本来就开放的么?
public static void main(String[] args){
String s1=new String("abc");
String s2=new String("abc");
if(s1.equals(s2))
System.out.println("s1=s2");//原因是s1的对象和s2的对象内容相同
else //(abc).equals(abc)==true
System.out.println("s1!=s2");
}
}
_________________________________________________________________________________________
class Func{
private String num;
public Func(String n){
num=n;
}
}
class TFunc{
public static void main(String[] args){
Func f1=new Func("abc");
Func f2=new Func("abc");
if(f1.equals(f2))
System.out.println("f1=f2");
else
System.out.println("f1!=f2");
}
}
当然会答应出f1!=f2,理由是“abc”只是对Func 的对象f1构造函数的参数,而不是对象的内容,不能看他相等就说内容一致,况且是String,不是Func型,如果是(f1.num).equals(f2.num)==true;
——————————————————————————————————————————--
我扩展如下
class Func{
public Func(){
}
public Func(Func n){
}
}
public class Test{
static Func f3,f4;
public static void main(String[] args){
Func f1=new Func(f3);System.out.println(f1);
Func f2=new Func(f4);System.out.println(f2);
if(f1.equals(f2))
System.out.println("f1=f2");
else
System.out.println("f1!=f2");
}
}
看看运行结果,就可以知道f1和f2是两个不同的对象返回来的名字不一样,应该也算内容吧
--------------------------------------------
建议楼主号召多些人来讨论,呵呵
Func n;
public Func(){
}
public Func(Func n){
this.n= n;
}
}
public class Test{
public static void main(String[] args){
Func f3=null,f4=null;
f4=f3;
Func f1=new Func(f3);System.out.println(f1);
Func f2=new Func(f4);System.out.println(f2);
if(f1.equals(f2))
System.out.println("f1=f2");
else
System.out.println("f1!=f2");
}
}
结果照样是f1!=f2
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = count;
if (n == anotherString.count) {
char v1[] = value;
char v2[] = anotherString.value;
int i = offset;
int j = anotherString.offset;
while (n-- != 0) {
if (v1[i++] != v2[j++])
return false;
}
return true;
}
}
return false;
}
也许,我猜测,equals是不是比较引用指向的内容,对于String,引用的地址不一样,可是指向的内容的地址却一样?(因为常量池的缘故?)对于一般的object,引用指向的内容的地址就不一样了,因为那可不是什么String,没什么常量池了。
常量池??????
这哪跟哪?
同意classjava(原始野人)
String类的equals,String类的equals是重写了Object的equals
你那书太烂,扔了!
equals方法,否则是不正确的。
2.一般的类都重写了equals方法,自定义类一般也要重写这个方法
但是如果要用于网络的话,仍然要重写equals.
equals 要看对象的具体怎么实现这个方法的就这么简单啊
对于Object类的equals方法,它提供了一个最最严密的实现,就是只有是同一对象时,equals方法才返回
true,也就是人们常说的引用比较而不是值比较.在具体子类(相对于Object来说)中,如果我们要进行对象的值比较,就必须实现自己的equals方法.
因为是菜鸟,所以在看初级的书。当然没有thinking in java写的那么详细。
因为是菜鸟,所以问一些初级的问题也不为过吧。
谢谢!!
“equals()”方法用于比较两个对象内容是否一致、(书上这么写的。呵呵!)
---------------------------------------
这样的翻译真是……
如果指明了是String类还好点
equals 是一个可以被子类所重载的方法,它默认的实现等同于 == ,但是如Integer,String都重写了该方法——如果内容相等返回true
private String num;
public Func(String n){
num=n;
}
}
class TFunc{
public static void main(String[] args){
Func f1=new Func("abc");
Func f2=new Func("abc");
if(f1.equals(f2))
System.out.println("f1=f2");
else
System.out.println("f1!=f2");
}
}
-------------------------------------------------------------
其中的f1、f2只是一个Func的实例,而Func类本身只是一个Object。
在Java里面的Object.equals(Object o)方法默认是比较reference的。
所以f1!=f2.
如果你要比较Object里面的内容,就只有重写equals()方法了。