equal 和 ==
刚才看了一下别人的博客,想加深一下对 equal 和 == 的了解。
总结了几点:
1.equal 每个类都有必要覆盖一下,对于String 类,已经覆盖,比较的是String对象的字符序列是否相等。
2.== 比较的是内存中两个对象是否为同一个,即地址是否相等;而对于基本数据类型,比较的是字面数值是否一样。
package com.java.test; import org.junit.Test; public class MyEqual { @Test
public void test() { int a1 = 10;
int a2 = 10; Integer b1 = 10;
Integer b2 = 10; String s1 = "abcd";
String s2 = "abcd";
String s3 = new String("abcd"); System.out.println(a1 == a2);//true System.out.println(b1 == b2);//true
System.out.println(b1.equals(b2));//true System.out.println(b1 == a1);//true
System.out.println(b1.equals(a1));//true System.out.println(s1 == s2);//true
System.out.println(s1 == s3);//false
System.out.println(s1.equals(s3));//true
} }
equal 和 ==的更多相关文章
- [LeetCode] Minimum Moves to Equal Array Elements II 最少移动次数使数组元素相等之二
Given a non-empty integer array, find the minimum number of moves required to make all array element ...
- [LeetCode] Minimum Moves to Equal Array Elements 最少移动次数使数组元素相等
Given a non-empty integer array of size n, find the minimum number of moves required to make all arr ...
- [LeetCode] Partition Equal Subset Sum 相同子集和分割
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
- Equal Sides Of An Array
参考:http://stackoverflow.com/questions/34584416/nested-loops-with-arrays You are going to be given an ...
- Int,Long比较重使用equal替换==
首先,==有很多限制,如Integer 类型的值在[-128,127] 期间,Integer 用 “==”是可以的(参考),超过范围则不行,那么使用equal则代替则完全ok public stati ...
- 无法解决 equal to 操作中 "SQL_Latin1_General_CP1_CI_AS" 和 "Chinese_PRC_CI_AS"
无法解决 equal to 操作中 "SQL_Latin1_General_CP1_CI_AS" 和 "Chinese_PRC_CI_AS" 之间 2011-0 ...
- LeetCode Minimum Moves to Equal Array Elements II
原题链接在这里:https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/ 题目: Given a non-empt ...
- LeetCode Minimum Moves to Equal Array Elements
原题链接在这里:https://leetcode.com/problems/minimum-moves-to-equal-array-elements/ 题目: Given a non-empty i ...
- conflict between "Chinese_PRC_CI_AI" and "Chinese_PRC_CI_AS" in the equal to operation
在SQL SERVICE做关联查询的时候遇到了"conflict between "Chinese_PRC_CI_AI" and "Chinese_PRC_CI ...
- why happen "WaitHandles must be less than or equal to 64"
一.背景: 在一个项目中碰到大数据插入的问题,一次性插入20万条数据(SQL Server),并用200个线程去执行,计算需要花费多少时间,因此需要等200个线程处理完成后,记录花费的时间,需要考虑的 ...
随机推荐
- 相对布局RelativeLayout
一. public class RelativeLayout extends ViewGroup java.lang.Object ↳ android.view.View ↳ an ...
- VS输入法问题
问题描述:启动VS,打开Winform等的界面设计,无法为控件输入中文,另外,运行程序,无法在TextBox等控件中输入中文: 本人的系统环境:Win7旗舰版,VS2008.VS2010和VS2012 ...
- centOS学习part3:远程工具VNC的安装与配置
0 上一篇(http://www.cnblogs.com/souvenir/p/3875484.html)我们介绍了通过yum安装JDK的实例,初步见识了yum命令的强大.本章我们将继续使用yum命令 ...
- Android核心分析 之十Android GWES之基本原理篇
Android GWES基本框架篇 我这里的GWES这个术语实际上从Microsoft 的Window上移植过来的,用GWES来表示Android的窗口事件系统不是那么准确,在Android中Wind ...
- QT中可以用QProgressBar或着QProgressDialog来实现进度条
QProgressBar的使用 首先在designer中拖一个按钮和进度条部件,按下面初始化 //补充:下面两句写在MainWindow的构造函数里进行初始化 ui->progressBar-& ...
- 私有虚函数的特点(C++和Java的机制还有所不同)
多态性与将实现多态的函数的访问限定符没有任何关系,private 函数仍然可以实现多态,它的指针仍然位于vtbl中,只不过该函数的多态一般只能在基类的内部由其他非虚函数调用该函数的时候反映出来,访问限 ...
- 3、Object对象的两大方法(hashCode-equals)总结
Object类是所有java类的父类. 用户定义了如下一个Person类 public class Person{} 在类定义中并没有明确继承Object类,但是编译器会自动的完成这个过程. 既然所有 ...
- SSIS ->> Reliability And Scalability
Error outputs can obviously be used to improve reliability, but they also have an important part to ...
- Oracle ->> 层级查询语句(hierarchical query)connect by
Oracle中的Connect By... Start With语句实现了递归查询或者树状查询. Connect By Prior 一方为起始(root)的ID 参考: http://www.360d ...
- 动态库加载出错,cannot restore segment prot after reloc: Permission denied
转自:taolinke的博客 项目中碰到的问题,编译好的so文件,放到其他机器上去加载,报了错误,cannot restore segment prot after reloc: Permission ...