java细节篇(==和equals的区别)
2024-10-12 19:43:57
1)当==两边是对象时(String,Integer...),两边比的都是地址
2)当==两边是基本类型时(int,float),两边比的都是值
3)默认equals比的是对象的地址,但是重写的话可以改变成比较值,String和Integer的equals就是重写过的
废话少说,Run!!!
package test; public class Test { public static void main(String[] args) { System.out.println("###############对象时比的都是地址################");
TestObj t = new TestObj();
TestObj t2 = new TestObj();
System.out.println(t==t2);//false
System.out.println(t.equals(t2));//false System.out.println("############基本类型[==]比的是值###########");
int a=1;
float b=1f;
System.out.println(a==b);//true
// System.out.println(a.equals(b)); 写法错误 System.out.println("##########Integer特殊(equals重写成比较值,但new时==还是比较地址)##########");
Integer i1=0;
Integer i2=0;
System.out.println(i1==i2);//true
System.out.println(i1.equals(i2));//true
Integer i3 = new Integer(0);
Integer i4 = new Integer(0);
System.out.println(i3==i4);//false
System.out.println(i3.equals(i4));//true
System.out.println(i1==i3);//false
System.out.println(i1.equals(i3));//true
System.out.println(i4.equals(i2));//true
int i5 = 0;
System.out.println(i1.equals(i5));//true 和基本int类型比是比值
System.out.println(i1==i5);//true 和基本int类型比也是比值
System.out.println(i3.equals(i5));//true new的和基本int类型比是比值
System.out.println(i3==i5);//true new的和基本int类型比也是比值
float f6 = 0f;//等同f6=0;
System.out.println(i1.equals(f6));//false 注意结果不同说明equals的重写只是对int类型时是比值
System.out.println(i1==f6);//true
System.out.println(i3.equals(f6));//false 注意结果不同说明equals的重写只是对int类型时是比值
System.out.println(i3==f6);//true System.out.println("=======String特殊=======");
String s1="a";
String s2="a";
System.out.println(s1==s2);
System.out.println(s1.equals(s2));
String s3 = new String("a");
String s4 = new String("a");
System.out.println(s3==s4);
System.out.println(s3.equals(s4));
System.out.println(s1==s3);
System.out.println(s1.equals(s3));
System.out.println(s4.equals(s2)); } }
对于Integer为什么只会对int作值的比较,查看源代码可见:
/**
* Compares this object to the specified object. The result is
* {@code true} if and only if the argument is not
* {@code null} and is an {@code Integer} object that
* contains the same {@code int} value as this object.
*
* @param obj the object to compare with.
* @return {@code true} if the objects are the same;
* {@code false} otherwise.
*/
public boolean equals(Object obj) {
if (obj instanceof Integer) {
return value == ((Integer)obj).intValue();
}
return false;
}
最新文章
- Yii 动作过滤的方法
- Maximo子表中增加附件功能
- elasticsearch installation guide
- HDU 5884 Sort -2016 ICPC 青岛赛区网络赛
- Swift3.0语言教程使用字符串创建和初始化字符串
- cf.295.B Two Buttons (bfs)
- [AngularJS] Accessing Services from Console
- hdu 3234 并查集
- Candence下对“跨页连接器(off-page connector)”进行批量重命名的方法
- LR(1)表生成算法演示程序
- 数据库(学习整理)----7--Oracle多表查询,三种join连接
- lodash的中文文档(不全)
- Handler.removeMessages的作用,有时候为什么一定要先remove一下呢
- 总结的git操作命令小抄集
- hihocoder_1014: Trie树(Trie树模板题)
- JAVA爬虫代码
- 算法-java代码实现希尔排序
- 页面性能优化-原生JS实现图片懒加载
- windows应用程序框架及实例
- myhabits where in foreach
热门文章
- java、javac -version不一致(java编译及运行环境不一致)的环境变量设置问题解决
- System.Web.UI.HtmlControls
- 前端jQuery之文档操作
- Exception occurred during processing request: The given object has a null identifier: com.zsn.crm.Model.SaleVisit; nested exception is org.hibernate.TransientObjectException: The given object has a nu
- PXE自动化安装CentOS6/7
- 爬虫学习(十四)——xpath项目实践
- 《Linux就该这么学》,刘小伙实在人,给打个广告
- angular常见问题总结
- 汇编:输出寄存器AX中的内容
- 开发工具cfree安装报错解决