我的理解:当a, b指向同一对象实例时,a==b一定返回true;
当a, b的各字段分别相等时,a.equals(b)一定返回true;
当a, b的各字段分别相等时,a.equals(b)一定返回true;
解决方案 »
- 请教各位大侠Java 7的try block-with-resource和之前的try有什么不同?
- 初学者问题··static boolean won[] = new boolean[1 << 9];
- 如何正确在for循环里的if语句中,写个break?
- sql server出错
- 菜鸟请教基础的小问题
- jdbc连数据库的问题,很奇怪
- 热烈欢迎新员工入司!
- windows怎么运行java程序
- 初学GUI,遇到的一个问题(附例程)
- JSP 中可以用<%=nm.getXXX()%>取得属性值,但是为什么不能用<jsp:getProperty name="nm" property="XXX"/>
- Exception in thread "main" java.lang.NoClassDefFoundError: install,我运行一个install.jar文件报这个错误?
- 请教:我的程序出错,我不知道如何解决?????
( 1 )
用 a.equals(b) 来进行判断a与b的内容是否相同,相同是true,相反是false.
用 a==b 来进行判断a, b是否指向同一对象,是返回true,不是返回false.
子类可以重写equals方法,比如String,就重写了equals,认为只要String的字符系列相同就可以返回true。
a==b用来比较是否同指一个对象!
(2)在同一内存块上当然,都是true了。当a ,b 指向不同内存块,但相等时,a.equals(b)返回true;a==b返回false.
如果想比较对象内容就必须重写equals().
java基本型别已经重写了equals().如果想比较自己的CLASS,就必须自己重写.
用a==b 它俩是不同的引数 (变量) (a和b是它们指向对像的一个引用而已,如果对象是电视机 a和b 就是摇控器(java编程思想) a和b 都可以控制同一台电视,但它们是不同的摇控器)
判断是否指向同一个对象用==
2:很大可能true true
true false
a==b比较的是a和b的地址是否相等
两者是有区别的
例如:
正如大家所说的==只是判断实力的地址是否相等,如果我们想比较实力的实际内容是否相等,我们可以使用equals()(因为单根继承所以每一个对象都有),而C++使用运算符重载。
public TestEquals(){}
public void testEquals(){
String testA = "abc";
String testB = "abc";
String testC = new String("abc");
String testD = new String("abc");
StringBuffer testE = new StringBuffer("abc");
StringBuffer testF = new StringBuffer("abc");
System.out.println("\"abc\" == \"abc\" is: "+ (testA == testB));
System.out.println("\"abc\" equals \"abc\" is: "+ (testA.equals(testB)));
System.out.println("new String(\"abc\") == new String(\"abc\") is: "+ (testC == testD));
System.out.println("new String(\"abc\") equals new String(\"abc\") is: "+ (testC.equals(testD)));
System.out.println("new StringBuffer(\"abc\") == new StringBuffer(\"abc\") is: "+ (testE == testF));
System.out.println("new StringBuffer(\"abc\") equals new StringBuffer(\"abc\") is: "+ (testE.equals(testF)));
System.out.println("\"abc\" == new String(\"abc\") is: "+ (testA == testC));
System.out.println("\"abc\" equals new String(\"abc\") is: "+ (testA.equals(testC)));
System.out.println("\"abc\" equals new StringBuffer(\"abc\") is: "+ (testA.equals( testE)));
System.out.println("new String(\"abc\") equals new StringBuffer(\"abc\") is: "+ (testC.equals( testE)));
}
public static void main(String[] args){
TestEquals test = new TestEquals();
test.testEquals();
}
}
a.equals(b)是检测这两个对象内容是否相等,a和b如果是两个对象但是内容一样,此时返回true,但是这种情况如果使用a==b就返回false
private String name = null;
private String address = null;
private String age = null;
public PersonEquals(){}
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
public void setAddress(String address){
this.address = address;
}
public String getAddress(){
return address;
}
public void setAge(String age){
this.age = age;
}
public String getAge(){
return age;
}
public boolean equals(Object aObject){
boolean theSame = false;
if(aObject instanceof PersonEquals){
if(this.name == ((PersonEquals)aObject).getName()){
theSame = true;
}
}
return theSame;
} public String toString(){
return "["+name+","+address+","+age+"]";
} public static void printResult(PersonEquals personA, PersonEquals personB, boolean result){
if(result){
System.out.println("The person"+personA.toString()+" and the person"+personB.toString()+" is the same!");
System.out.println("Because their names are 'equals' under my control:) ");
}else{
System.out.println("The person"+personA.toString()+" and the person"+personB.toString()+" is not the same!");
System.out.println("Because their names are not 'equals' under my control:) ");
}
} public static void main(String[] args) {
PersonEquals testA = new PersonEquals();
testA.setName("abc");
testA.setAddress("beijing");
testA.setAge("80");
PersonEquals testB = new PersonEquals();
testB.setName("abc");
testB.setAddress("shanghai");
testB.setAge("8");
PersonEquals testC = new PersonEquals();
testC.setName("def");
testC.setAddress("beijing");
testC.setAge("80");
Object notPerson = new String("abc");
printResult(testA, testB, testA.equals(testB));
printResult(testA, testC, testA.equals(testC));
printResult(testC, testB, testC.equals(testB));
System.out.println("The same of person"+testA.toString()+" and "+notPerson.toString()+" is "+testA.equals(notPerson));
}
}
很多人把某个类的equals方法泛化了,有误导作用
a == b 比较a,b的instance指向的是否是同一个对象例子:
1.
a = "aaa";
b = "aaa";
a.equals(b) //true
a == b // true2.
a = "aaa";
b = new String("aaa");
a.equals(b) // true
a == b // false
b = "aaa";为什么a == b // true呢?
public class EqualsMethod{
public static void main(String[] args){
Integer n1=new Integer(47);
Integer n2=new Integer(47);
System.out.println(n1.equals(n2));
}
}
结果为true.
equals()的默认行为是比较句柄。
而==是判断基本数据类型是否想等的
例如String i="hello world";
然后你又有了String d="hello world";他们的内存是相同的
说白了就是 equals在内存相同是比较的都是true,但是在内存空间不同时则是比较的值
否则会有异常
a==b 比较对象是否相等
在java中,类的实例都被重载,例如:
Integer i=new Integer(23);
Integer s=new Integer(23);
当用i.equals(s)时, true
用i==s时, false
当你创建用户自定义类时,你的输出就不一样啦
例如
aa a=new aa("hh")
aa b=new aa(hh")
用a.equals(b)时, 为false