==和.equals()的区别
来源:互联网 发布:什么软件可以看美剧 编辑:程序博客网 时间:2024/04/30 06:15
原文链接 How do I compare strings in Java?
- == 执行的是 引用相等性的比较
- .equals() 执行的是 值相等性的比较
Objects.equals() 相比于 .equals()可能是更好的选择,该方法会在比较前检查对象是否为null。
// These two have the same valuenew String("test").equals("test") // --> true // ... but they are not the same objectnew String("test") == "test" // --> false // ... neither are thesenew String("test") == new String("test") // --> false // ... but these are because literals are interned by // the compiler and thus refer to the same object"test" == "test" // --> true // ... but you should really just call Objects.equals()Objects.equals("test", new String("test")) // --> trueObjects.equals(null, "test") // --> false
附上String::equals()源码:
public boolean equals(Object anObject) { if (this == anObject) { return true; } if (anObject instanceof String) { String anotherString = (String)anObject; int n = value.length; if (n == anotherString.value.length) { char v1[] = value; char v2[] = anotherString.value; int i = 0; while (n-- != 0) { if (v1[i] != v2[i]) return false; i++; } return true; } } return false; }
0 0
- “=”和“equals()”的区别
- java ==和equals、equals和hashCode的区别
- 关于“==”和“equals”的区别
- 关于Equals 和== 的区别
- equals和==的区别
- equals 和 == 的区别
- equals 和 == 的区别
- ==和Equals的区别
- equals 和 == 的区别
- equals 和 == 的区别
- equals和==的区别
- equals 和 == 的区别
- equals和==的区别
- 关于==和equals的区别
- Equals和“==”的区别
- == 和equals() 的区别
- equals("")和“==”的区别
- equals 和 == 的区别
- 解决magento保存产品时耗时很长的问题
- JNI语法 JNI参考 JNI函数大全
- Excel导入文件缓存进入数据库
- windows 环境 重启tomcat
- 代码“微优化”
- ==和.equals()的区别
- 属性动画的基本用法
- Netbeans 注释模板配置
- K-Means聚类算法原理及实现
- cocos2dx暂停某个action动作并恢复&简单的js-binding
- CUDA 学习(十)、寄存器用法
- Ubuntu problems apt-get:Package has no installation candidate
- 【iOS】Xcode7以后的添加empty project方法
- 获取所有子产品 和 通过款号获取所有可配置产品