欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

代码详解java里的“==”和“equels”区别

程序员文章站 2022-06-05 08:45:46
测试1: 先看一组string类型比较,废话不多说,直接上代码: public class test { public static void...

测试1:

先看一组string类型比较,废话不多说,直接上代码:

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)");
    }
  }
}

输出结果:

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类型的测试:

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");
    }
  }
}

输出的结果:

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的源码。

/**
   * 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了一个新的对象,故不等。