equals 和 ==

1.基本数据类型

   byte,short,char,int,long,float,double,boolean 用==比较的是他们的值

2.复合数据类型

   == 比较的是内存中的地址

   java 对象默认都继承Object这个基类, equals 方法使用的是 == 也是比较的对象地址。

1     public boolean equals(Object var1) {
2         return this == var1;
3     }
View Code

  String,Integer,Long,Date在这些类当中equals有其自身的实现,而不再是比较类在堆内存中的存放地址了

 

 1 public boolean equals(Object anObject) {
 2         if (this == anObject) {
 3             return true;
 4         }
 5         if (anObject instanceof String) {
 6             String anotherString = (String)anObject;
 7             int n = value.length;
 8             if (n == anotherString.value.length) {
 9                 char v1[] = value;
10                 char v2[] = anotherString.value;
11                 int i = 0;
12                 while (n-- != 0) {
13                     if (v1[i] != v2[i])
14                         return false;
15                     i++;
16                 }
17                 return true;
18             }
19         }
20         return false;
21     }
String
1  public boolean equals(Object obj) {
2      if (obj instanceof Integer) {
3          return value == ((Integer)obj).intValue();
4      }
5      return false;
6  }
Integer
1 public boolean equals(Object obj) {
2     if (obj instanceof Long) {
3         return value == ((Long)obj).longValue();
4     }
5     return false;
6 }
Long
1     public boolean equals(Object obj) {
2         return obj instanceof Date && 
3                 getTime() == ((Date) obj).getTime();
4     }
Date

复合数据类型之间进行equals比较,在没有覆写equals方法的情况下,比较的是他们在内存中的存放地址,比较后的结果跟双等号(==)的结果相同。

Integer 和 Long都有缓存区域 【-128,127】 Integer的最大值可以在虚拟机初始化的时候设置

 

 1 private static class IntegerCache {
 2         static final int low = -128;
 3         static final int high;
 4         static final Integer cache[];
 5 
 6         static {
 7             // high value may be configured by property
 8             int h = 127;
 9             String integerCacheHighPropValue =
10                 sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
11             if (integerCacheHighPropValue != null) {
12                 try {
13                     int i = parseInt(integerCacheHighPropValue);
14                     i = Math.max(i, 127);
15                     // Maximum array size is Integer.MAX_VALUE
16                     h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
17                 } catch( NumberFormatException nfe) {
18                     // If the property cannot be parsed into an int, ignore it.
19                 }
20             }
21             high = h;
22 
23             cache = new Integer[(high - low) + 1];
24             int j = low;
25             for(int k = 0; k < cache.length; k++)
26                 cache[k] = new Integer(j++);
27 
28             // range [-128, 127] must be interned (JLS7 5.1.7)
29             assert IntegerCache.high >= 127;
30         }
31 
32         private IntegerCache() {}
33     }
View Code

 

1 Integer a = 200;
2 Integer b = 200;
3 Long d = 200L;
4 Long e = 200L;
5 System.out.println(a == b); //false
6 System.out.println(d == e); //fasle
View Code

 

posted @ 2018-12-05 14:12  YoungXy  阅读(115)  评论(0编辑  收藏  举报