Java中的==和equals

1.基本数据类型

  byte ,short ,int ,long ,double ,float,boolean,char

  他们之间的比较,应用双等号(==),比较的是他们的值。

2.复合数据类型(类)

  当他们用(==)进行比较的时候,比较的是他们在内存中的存放地址,所以,除非是同一个new出来的对象,他们的比较后的结果为true,否则比较后结果为false。JAVA当中所有的类都是继承于Object这个基类的,在Object中的基类中定义了一个equals的方法,Object的equals源码:

1 public boolean equals(Object obj) {
2         return (this == obj);
3     }

这个方法的初始行为是比较对象的内存地 址,但在一些类库当中这个方法被覆盖掉了,如String,Integer,Date在这些类当中equals有其自身的实现,而不再是比较类在堆内存中的存放地址了。

  对于复合数据类型之间进行equals比较,在没有覆写equals方法的情况下,他们之间的比较还是基于他们在内存中的存放位置的地址值的,因为Object的equals方法也是用双等号(==)进行比较的,所以比较后的结果跟双等号(==)的结果相同。

范例代码:

 1 public class Test {
 2     public static void test01() {
 3         String s1 = "hello";
 4         String s2 = "hello";
 5         System.out.print("test01: ");
 6         if (s1 == s2) {
 7             System.out.print("s1==s2" + "  ");
 8         } else {
 9             System.out.print("s1!=s2" + "  ");
10         }
11         if (s1.equals(s2)) {
12             System.out.print("s1 equals s2");
13         } else {
14             System.out.print("s1 not equals s2");
15         }
16     }
17 
18     public static void test02() {
19         String s1 = "hello";
20         String s2 = new String("hello");
21         System.out.print("\ntest02: ");
22         if (s1 == s2) {
23             System.out.print("s1==s2" + "  ");
24         } else {
25             System.out.print("s1!=s2" + "  ");
26         }
27         if (s1.equals(s2)) {
28             System.out.print("s1 equals s2");
29         } else {
30             System.out.print("s1 not equals s2");
31         }
32     }
33 
34     public static void test03() {
35         String s1 = "hello";
36         String s2 = new String("hello");
37         System.out.print("\ntest03: ");
38         s2 = s2.intern();
39         if (s1 == s2) {
40             System.out.print("s1==s2" + "  ");
41         } else {
42             System.out.print("s1!=s2" + "  ");
43         }
44         if (s1.equals(s2)) {
45             System.out.print("s1 equals s2");
46         } else {
47             System.out.print("s1 not equals s2");
48         }
49     }
50 
51     public static void main(String[] args) {
52         test01();
53         test02();
54         test03();
55         int i = 0;
56         Integer j = new Integer(0);
57         System.out.println("\n" + (i == j));
58         System.out.println(j.equals(i));
59     }
60 }

 

输出结果:

test01: s1==s2  s1 equals s2
test02: s1!=s2  s1 equals s2
test03: s1==s2  s1 equals s2
true
true

对于test03返回s1==s2,因为s2.intern()返回的是“hello”,只不过返回的是字符串池中的“hello”,查看jdk文档可以发现对String的intern()方法定义如下:

public String intern()
返回字符串对象的规范化表示形式。

一个初始为空的字符串池,它由类 String 私有地维护。

当调用 intern 方法时,如果池已经包含一个等于此 String 对象的字符串(用 equals(Object) 方法确定),则返回池中的字符串。否则,将此 String 对象添加到池中,并返回此 String 对象的引用。

它遵循以下规则:对于任意两个字符串 s 和 t,当且仅当 s.equals(t) 为 true 时,s.intern() == t.intern() 才为 true

所有字面值字符串和字符串赋值常量表达式都使用 intern 方法进行操作。

返回:一个字符串,内容与此字符串相同,但一定取自具有唯一字符串的池。

 

执行这个方法的时候,它会检查字符串池中是否存在“hello”字符串,如果存在,就返回池中的字符串,如果不存在就将“hello”添加到池中,再返回到它的引用。

 

参考:http://www.cnblogs.com/zhxhdean/archive/2011/03/25/1995431.html

 

 

posted @ 2017-09-05 22:13  zzzzzhangrui  阅读(164)  评论(0)    收藏  举报