== 与 equals
==:他的作用时判断两个对象的地址是不是相等,即判断两个对象是不是同一个对象(基本数据类型比较的是值,引用数据类型比较的是内存地址)
equals:
1>没有重写了equals()方法,等价于"=="
2>重写了equals()方法,比较两个对象的内容是否相同,内容相同返回true.
-
String 中的 equals 方法是被重写过的,因为 object 的 equals 方法是比较的对象的内存地址,而 String 的 equals 方法比较的是对象的值。
-
当创建 String 类型的对象时,虚拟机会在常量池中查找有没有已经存在的值和要创建的值相同的对象,如果有就把它赋给当前引用。如果没有就在常量池中重新创建一个 String 对象。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26/** * @author * @date 2020/12/6 12:41 */ public class S { public static void main(String[] args) { String x="Jarvis"; String y="Jarvis"; String z=new String("Jarvis"); System.out.println("x = " + x.hashCode()); System.out.println("y = " + x.hashCode()); System.out.println("z = " + z.hashCode()); System.out.println(x); System.out.println(y); System.out.println(z); System.out.println(System.identityHashCode(x)); System.out.println(System.identityHashCode(y)); System.out.println(System.identityHashCode(z)); System.out.println(x == y); System.out.println(x == z); System.out.println("false是因为hashcode也是被重写了"); System.out.println("x.equals(z) = " + x.equals(z)); } }
关于运算符优先级的问题:
最后
以上就是孤独酸奶最近收集整理的关于== 与 equals的全部内容,更多相关==内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复