激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - 代碼詳解java里的“==”和“equels”區別

代碼詳解java里的“==”和“equels”區別

2021-04-04 14:39Haker_楓 Java教程

本篇文章通過實例代碼給大家詳細解釋了java里的“==”和“equels”區別,對此有興趣的朋友跟著小編一起學習下。

測試1:

先看一組String類型比較,廢話不多說,直接上代碼:

java" id="highlighter_339495">
?
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
public class Test {
 
  public static void main(String[] args) {
    String a = "java書苑";
    String b = "java書苑";
    String c = new String("java書苑");
    String d = new String("java書苑").intern();
 
    if(a == b){
      System.out.println("a == b");
    }else{
      System.out.println("a != b");
    }
 
    if(a.equals(b)){
      System.out.println("a.equals(b)");
    }else{
      System.out.println("!a.equals(b)");
    }
 
    if(a == c){
      System.out.println("a == c");
    }else{
      System.out.println("a != c");
    }
 
    if(a.equals(c)){
      System.out.println("a.equals(c)");
    }else{
      System.out.println("!a.equals(c)");
    }
 
    if(a == d){
      System.out.println("a == d");
    }else{
      System.out.println("a != d");
    }
 
    if(a.equals(d)){
      System.out.println("a.equals(d)");
    }else{
      System.out.println("a.equals(d)");
    }
  }
}

輸出結果:

?
1
2
3
4
5
6
a == b
a.equals(b)
a != c
a.equals(c)
a == d
a.equals(d)

總結:

結果a == b:程序在運行的時候會創建一個字符串緩沖池,在String a = “java書苑”時, “java書苑”被放到了字符串緩沖池中,當 String b = “java書苑” 創建字符串的時候,程序首先會在這個String緩沖池中尋找相同值的對象,所以在b被創建的時候,程序找到了具有相同值的a,將b 引用 a 所引用的對象。所以a和b引用的同一個對象,故a == b。

結果a != c:String c = new String(“java書苑”)時new了一個新的對象,故不從String緩沖池尋找,二十直接創建一個新的對象。所以a != c。

結果a == d :當調用 intern 方法時,如果池已經包含一個等于此 String 對象的字符串(該對象由 equals(Object) 方法確定),則返回池中的字符串。否則,將此 String 對象添加到池中,并且返回此 String 對象的引用。所有d調用的同樣是a的對象。
equals比較的是值,故值一樣時便相等。

測試2:

這是一組int類型和Integer類型的測試:

?
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public class Test {
 
  public static void main(String[] args) {
 
    int a = 127;
    int a1 = 127;
 
    int b = 128;
    int b1 = 128;
 
 
    Integer c = 127;
    Integer c1 = 127;
 
    Integer d = 128;
    Integer d1 = 128;
 
    if(a == a1){
      System.out.println("a == a1");
    }else{
       System.out.println("a != a1");
    }
 
    if(b == b1){
      System.out.println("b == b1");
    }else{
       System.out.println("b != b1");
    }
 
    if(c == c1){
      System.out.println("c == c1");
    }else{
       System.out.println("c != c1");
    }
 
    if(d == d1){
      System.out.println("d == d1");
    }else{
       System.out.println("d != d1");
    }
  }
}

輸出的結果:

?
1
2
3
4
a == a1
b == b1
c == c1
d != d1

結果”a == a1”和”b == b1”:int 是基本類型,直接存數值,而integer是對象,用一個引用指向這個對象,多以比較的時候”a == a1”和”b == b1”。

結果“c == c1”和“d != d1”這里可能有人會有疑問,為什么“d != d1”.我們一起看一下Integer的源碼。

?
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
   * Cache to support the object identity semantics of autoboxing for values between
   * -128 and 127 (inclusive) as required by JLS.
   *
   * The cache is initialized on first usage. The size of the cache
   * may be controlled by the -XX:AutoBoxCacheMax=<size> option.
   * During VM initialization, java.lang.Integer.IntegerCache.high property
   * may be set and saved in the private system properties in the
   * sun.misc.VM class.
   */
 
  private static class IntegerCache {
    static final int low = -128;
    static final int high;
    static final Integer cache[];
 
    static {
      // high value may be configured by property
      int h = 127;
      String integerCacheHighPropValue =
        sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
      if (integerCacheHighPropValue != null) {
        int i = parseInt(integerCacheHighPropValue);
        i = Math.max(i, 127);
        // Maximum array size is Integer.MAX_VALUE
        h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
      }
      high = h;
 
      cache = new Integer[(high - low) + 1];
      int j = low;
      for(int k = 0; k < cache.length; k++)
        cache[k] = new Integer(j++);
    }
 
    private IntegerCache() {}
  }
 
  /**
   * Returns an {@code Integer} instance representing the specified
   * {@code int} value. If a new {@code Integer} instance is not
   * required, this method should generally be used in preference to
   * the constructor {@link #Integer(int)}, as this method is likely
   * to yield significantly better space and time performance by
   * caching frequently requested values.
   *
   * This method will always cache values in the range -128 to 127,
   * inclusive, and may cache other values outside of this range.
   *
   * @param i an {@code int} value.
   * @return an {@code Integer} instance representing {@code i}.
   * @since 1.5
   */
  public static Integer valueOf(int i) {
    assert IntegerCache.high >= 127;
    if (i >= IntegerCache.low && i <= IntegerCache.high)
      return IntegerCache.cache[i + (-IntegerCache.low)];
    return new Integer(i);
  }

結論:這里 Integer 會初始化一個[-128,127]的常量池,如果數值在這個范圍時,則引用的是同一個對象,如果不在這個范圍,通過源碼可以看出返回的是new了一個新的對象: return new Integer(i);

所以,結果“c == c1”是引用了同一個對象,結果“d != d1”,是new了一個新的對象,故不等。

原文鏈接:http://blog.csdn.net/qq_34988304/article/details/78711473

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 一级免费在线视频 | 色黄视频免费观看 | 成人在线精品视频 | 性爱视频在线免费 | 国产成年人网站 | 成人男女免费视频 | av国产在线被下药迷网站 | 欧美在线电影 | 中文字幕观看 | 99亚洲伊人久久精品影院红桃 | 亚洲免费高清 | 欧美一区二区黄色 | 极品xxxx欧美一区二区 | 宅男噜噜噜66国产免费观看 | 免费在线观看中文字幕 | 日韩做爰视频免费 | av在线免费不卡 | 久久69精品久久久久久国产越南 | 麻豆视频在线免费观看 | 国产成人强伦免费视频网站 | 涩涩激情网 | 九色com | 国产久草视频在线 | 亚洲精中文字幕二区三区 | 操碰| 亚洲婷婷日日综合婷婷噜噜噜 | 久久国产精品二国产精品中国洋人 | 欧美一级免费高清 | 主播粉嫩国产在线精品 | 国产精品久久久久久影院8一贰佰 | 99riav视频一区二区 | 成人免费网站在线观看视频 | 精品1 | 成人福利视频导航 | av中文字幕免费在线观看 | 久久久精品精品 | 成人免费一区 | 日韩大片在线永久观看视频网站免费 | 色操网| 国产一级毛片高清视频完整版 | 亚洲va久久久噜噜噜久牛牛影视 |