==和equal()的区别
2015-09-16 00:00
211 查看
摘要: 有关于==和equal()的区别,==不多说比较的是基本类型的值和引用类型的地址值;很多人应该是困惑equal(),
有关于==和equal()的区别,==不多说比较的是基本类型的值和引用类型的地址值;很多人应该是困惑equal(),这个其实equal()是Object中的默认方法。String只是继承和重写了Object中equal()方法,说白了:在Object中比较的是两个对象的地址值,在String中比较的是两个的字符串的内容这个在API里都有体现。
以 T1
String s1 = new String("hello");
String s2 = new String("hello");
System.out.println(s1 == s2); //false
System.out.println(s1.equals(s2));//true
T2
String s3 = new String("hello");
String s4 = "hello";
System.out.println(s3 == s4);//false
System.out.println(s3.equals(s4));//true
T3
String s5 = "hello";
String s6 = "hello";
System.out.println(s5 == s6);//true
System.out.println(s5.equals(s6));//true
为了便于理解我用以T4作为注释
T4
String s = new String(“hello”);
String s = “hello”;的区别?
第一句话造了两个对象。(一个或者两个)
第二句话造了一个对象。(也可能没有)
然后这个也就有了一个关于拼接是否相等的问题
String s1 = "hello";
String s2 = "world";
String s3 = "helloworld";
System.out.println(s3 == s1 + s2);//false
System.out.println((s1+s2).equals(s3));//true
System.out.println("helloworld" == s1 + s2);//false
System.out.println("helloworld" == "hello" + "world"); // true
在这个里面有一个细节:字符串常量相加和变量相加的区别:
变量拼接,先开空间,然后拼接
常量拼接,先拼接,找,存在,就不开空间了,否则就开空间存储。
数组中的这个equals和==的问题
int arr1[]={1,2,3};
int arr2[]={1,2,3};
System.out.println(arr1.equals(arr2));//false这个并不是重写的方法
System.out.println(Arrays.equals(arr1,arr2));//true
System.out.println(arr1==arr2);//false
有关于==和equal()的区别,==不多说比较的是基本类型的值和引用类型的地址值;很多人应该是困惑equal(),这个其实equal()是Object中的默认方法。String只是继承和重写了Object中equal()方法,说白了:在Object中比较的是两个对象的地址值,在String中比较的是两个的字符串的内容这个在API里都有体现。
以 T1
String s1 = new String("hello");
String s2 = new String("hello");
System.out.println(s1 == s2); //false
System.out.println(s1.equals(s2));//true
T2
String s3 = new String("hello");
String s4 = "hello";
System.out.println(s3 == s4);//false
System.out.println(s3.equals(s4));//true
T3
String s5 = "hello";
String s6 = "hello";
System.out.println(s5 == s6);//true
System.out.println(s5.equals(s6));//true
为了便于理解我用以T4作为注释
T4
String s = new String(“hello”);
String s = “hello”;的区别?
第一句话造了两个对象。(一个或者两个)
第二句话造了一个对象。(也可能没有)
然后这个也就有了一个关于拼接是否相等的问题
String s1 = "hello";
String s2 = "world";
String s3 = "helloworld";
System.out.println(s3 == s1 + s2);//false
System.out.println((s1+s2).equals(s3));//true
System.out.println("helloworld" == s1 + s2);//false
System.out.println("helloworld" == "hello" + "world"); // true
在这个里面有一个细节:字符串常量相加和变量相加的区别:
变量拼接,先开空间,然后拼接
常量拼接,先拼接,找,存在,就不开空间了,否则就开空间存储。
数组中的这个equals和==的问题
int arr1[]={1,2,3};
int arr2[]={1,2,3};
System.out.println(arr1.equals(arr2));//false这个并不是重写的方法
System.out.println(Arrays.equals(arr1,arr2));//true
System.out.println(arr1==arr2);//false
相关文章推荐
- JavaScript中的操作符==与===介绍
- C#值类型、引用类型中的Equals和==的区别浅析
- C#中重载相等(==)运算符示例
- javascript中if和switch,==和===详解
- java中 == 与 equal 的区别讲解
- Java中==与equals的区别小结
- 基于Java字符串 "==" 与 "equals" 的深入理解
- Python 中的 == 与 is 及其他
- Java中equals和==的区别
- StopWatch 学习
- php == 操作符带来的安全问题
- python中if __name__ == "__main__":用法解析
- java中的==、equals()、hashCode()源码分析
- 【编程好习惯】将常量放在“==”之前
- "=="和 equals 方法究竟有什么区别?
- "=="和 equals 方法究竟有什么区别?
- 由装箱引发的——Integer比较的来龙去脉
- windosw7 Hosts文件的位置
- 简单谈一下java中的==与equals()方法
- ==还有equals(),输出true还是false?