前几天去一公司笔试,题目都很基础,但是都很让人郁闷,总觉得会又不是十分透彻的感觉,特别是equals和==的一些题目,回来自己测试了一些,和大家分享!
首先是几个基本类型的用法:
package
com.bulagy;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
/** */
/**
* @author bulargy
*
*/
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
public
class
EqualsDemo
...
{
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/** *//**
* @param args
*/
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public static void main(String[] args) ...{
boolean boo1 = true;
boolean boo2 = true;
boolean boo3 = false;
boolean boo4 ;
boolean boo5 = boo1;
//boolean boo5 = new boolean();//不合法的定义
System.out.println("boo1==boo2 ? "+(boo1==boo2));
System.out.println("boo1==boo3 ? "+(boo1==boo3));
//System.out.println("boo1==boo2 ? "+(boo1.equals(boo2)));//没有equals方法
System.out.println("boo1==boo5 ? "+(boo1==boo5));
//System.out.println("boo3==boo4 ? "+(boo3==boo4));//boo4未初始化
System.out.println();
String s1 = "abcd";
String s2 = new String("abcd");
String s3 = "abcd";
String s4 = new String("abcd");
String s5 = s1;
String s6 = s2;
String s7;
String s8 = new String();
String s9 ="";
System.out.println("s1==s2 ? "+(s1==s2));
System.out.println("s1.equals(s2) ? "+s1.equals(s2));
System.out.println("s1==s3 ? "+(s1==s3));
System.out.println("s1.equals(s3) ? "+s1.equals(s3));
System.out.println("s2==s4 ? "+(s2==s4));
System.out.println("s2.equals(s4) ? "+s2.equals(s4));
System.out.println("s1==s5 ? "+(s1==s5));
System.out.println("s1.equals(s5) ? "+s1.equals(s5));
System.out.println("s2==s6 ? "+(s2==s6));
System.out.println("s2.equals(s6) ? "+s2.equals(s6));
//System.out.println("s7==s8 ? "+(s7==s8));//s7未初始化
//System.out.println("s7.equals(s7) ? "+s7.equals(s8));//s7未初始化
System.out.println("s8==s9 ? "+(s8==s9));
System.out.println("s8.equals(s9) ? "+s8.equals(s9));
System.out.println("s8= "+s8);
System.out.println("s9= "+s9);
System.out.println();
int i1 = 11;
int i2 = new Integer(11);
int i3 = 11;
int i4 = new Integer(11);
int i5 = i1;
int i6 = i2;
int i7;
int i8 = new Integer(0);
int i9 = 0;
System.out.println("i1==i2 ? "+(i1==i2));
//System.out.println("i1.equals(i2) ? "+i1.equals(i2));//没有equals方法
System.out.println("i1==i3 ? "+(i1==i3));
//System.out.println("i1.equals(i3) ? "+i1.equals(i3));//没有equals方法
System.out.println("i2==i4 ? "+(i2==i4));
//System.out.println("i2.equals(i4) ? "+i2.equals(i4));//没有equals方法
System.out.println("i1==i5 ? "+(i1==i5));
//System.out.println("i1.equals(i5) ? "+i1.equals(i5));//没有equals方法
System.out.println("i2==i6 ? "+(i2==i6));
//System.out.println("i2.equals(i6) ? "+i2.equals(i6));//没有equals方法
//System.out.println("i7==i8 ? "+(i7==i8));//i7未初始化
//System.out.println("i7.equals(i7) ? "+i7.equals(i8));//没有equals方法
System.out.println("i8==i9 ? "+(i8==i9));
//System.out.println("i8.equals(i9) ? "+i8.equals(i9));//没有equals方法
System.out.println("i8= "+i8);
System.out.println("i9= "+i9);
System.out.println();
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
运行结果:
boo1==boo2 ? true
boo1==boo3 ? false
boo1==boo5 ? true
s1==s2 ? false
s1.equals(s2) ? true
s1==s3 ? true
s1.equals(s3) ? true
s2==s4 ? false
s2.equals(s4) ? true
s1==s5 ? true
s1.equals(s5) ? true
s2==s6 ? true
s2.equals(s6) ? true
s8==s9 ? false
s8.equals(s9) ? true
s8=
s9=
i1==i2 ? true
i1==i3 ? true
i2==i4 ? true
i1==i5 ? true
i2==i6 ? true
i8==i9 ? true
i8= 0
i9= 0
小结:
1、任何对象使用之前都要初始化,仅仅声明是不够的;
2、只有String等对象形式数据才有equals方法,基本数据类型是没有的;
然后是一个数组的:
package
com.bulagy;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
import
java.util.Arrays;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
public
class
EqualsArrayDemo
...
{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public static void main(String[] args) ...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
int[] arr1 = ...{1, 2, 3, 4, 5};
int[] tmp1 = arr1;
int[] tmp2 = arr1;
System.out.print("通过tmp1取出数组值:");
for(int i = 0; i < tmp1.length; i++)
System.out.print(tmp1[i] + " ");
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
System.out.print(" 通过tmp2取出数组值:");
for(int i = 0; i < tmp2.length; i++)
System.out.print(tmp2[i] + " ");
tmp1[2] = 9;
System.out.print(" 通过tmp1取出数组值:");
for(int i = 0; i < tmp1.length; i++)
System.out.print(tmp1[i] + " ");
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
System.out.print(" 通过tmp2取出数组值:");
for(int i = 0; i < tmp2.length; i++)
System.out.print(tmp2[i] + " ");
System.out.println();
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
int[] arr2 = ...{1, 2, 3, 4, 5};
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
int[] arr3 = ...{5, 6, 7, 8, 9, 10, 11};
int[] tmp3 = arr2;
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
int[] arr4 = new int [] ...{1, 2, 3, 4, 5};
System.out.print("使用tmp3取出arr2中的元素:");
for(int i = 0; i < tmp3.length; i++)
System.out.print(tmp3[i] + " ");
tmp3 = arr3;
System.out.print(" 使用tmp3取出arr3中的元素:");
for(int i = 0; i < tmp3.length; i++)
System.out.print(tmp3[i] + " ");
System.out.println();
System.out.println("arr1==arr2 ? "+(arr1==arr2));
System.out.println("arr1.equals(arr2) ? "+arr1.equals(arr2));
System.out.println("Arrays.equals(arr1,arr2) ? "+Arrays.equals(arr1,arr2));
System.out.println("tmp1==tmp2 ? "+(tmp1==tmp2));
System.out.println("tmp1.equals(tmp2) ? "+tmp1.equals(tmp2));
System.out.println("Arrays.equals(tmp1,tmp2) ? "+Arrays.equals(tmp1,tmp2));
System.out.println("arr2==arr4 ? "+(arr2==arr4));
System.out.println("arr2.equals(arr4) ? "+arr2.equals(arr4));
System.out.println("Arrays.equals(arr2,arr4) ? "+Arrays.equals(arr2,arr4));
}
}
运行结果:
通过tmp1取出数组值:1 2 3 4 5
通过tmp2取出数组值:1 2 3 4 5
通过tmp1取出数组值:1 2 9 4 5
通过tmp2取出数组值:1 2 9 4 5
使用tmp3取出arr2中的元素:1 2 3 4 5
使用tmp3取出arr3中的元素:5 6 7 8 9 10 11
arr1==arr2 ? false
arr1.equals(arr2) ? false
Arrays.equals(arr1,arr2) ? false
tmp1==tmp2 ? true
tmp1.equals(tmp2) ? true
Arrays.equals(tmp1,tmp2) ? true
arr2==arr4 ? false
arr2.equals(arr4) ? false
Arrays.equals(arr2,arr4) ? true
小结:
1、=号仅仅是数据对象在内存中的引用而已;
2、equals和==都是比较是不是引用自同一个对象,不是比较值的;
3、比较值应该用Arrays.equals()方法,如果是多维数组要用Array.deepEquals()方法;
先就这个多,算是把 equals和==在心里有比较明确的认识了,本人很菜,如果有不对的地方请大家多指教,如果有补充的欢迎大家补充!共同学习,一起进步!