Java Object类

Java Object类

java.lang.Object

说明

public class Object

Class Object is the root of the class hierarchy. Every class has Object as a superclass. All objects, including arrays, implement the methods of this class.

类中方法

(这里不讨论与线程、反射、垃圾回收有关的方法)

  1. public boolean equals(Object obj)判断两个对象是否相等

    equals方法的特点:
    1. 自反性: 任何非空引用x,x.equals(x)返回为true;
    2. 对称性:任何非空引用x和y,x.equals(y)返回true当且仅当y.equals(x)返回true;
    3. 传递性:任何非空引用x和y,如果x.equals(y)返回true,并且y.equals(z)返回true,那么x.equals(z)返回true;
    4. 一致性:两个非空引用x和y,x.equals(y)的多次调用应该保持一致的结果,(前提条件是在多次比较之间没有修改x和y用于比较的相关信息);
    5. 约定对于任何非空引用x,x.equals(null)应该返回为false
    ==表示变量值完全相同。即,对于基础类型值相等,对于引用类型地址相同
    equals一般用于判断对象的内容是否完全相同。Object类的实现:

    public boolean equals(Object obj) {
    return (this == obj);
    }
    可以根据需求覆写,一般覆写的形式:

    @Override
    public boolean equals(Object otherObj) {
    if (this == otherObj){ // 是否是同一个对象的引用
    return true;
    }
    if (otherObj == null){ // 是否为null
    return false;
    }
    if(getClass() != otherObj.getClass()){ // 是否是相同类型
    return false;
    }
    User other = (User)otherObj; // 强制类型转换
    return id.equals(other.id) && name.equals(other.name); // 根据需求对比对象内容
    }

  2. public int hashCode()返回一个整形数值,表示该对象的hashCode值
    hashCode值的作用:增强Hash表性能,主要针对集合类,两个相等的对象其hashCode值一定是相等的。
    hashCode方法返回的并不是物理内存地址,甚至不是逻辑地 址。Hash表本身存在冲突,所以两个相等的对象其hashCode值并不一定是相等的。
    若对象使用Hash表存储,比较两个对象时将使用hashCode方法,因此重写equals方法时非常有必要重写hashCode方法.
    可以使用org.apache.commons.lang.builder.HashCodeBuilder通过反射机制来自动计算对象的hashCode值.
    通用的重写hashCode形式:

    1. 初始化一个整形变量,为此变量赋予一个非零的常数值,比如int result = 17;
    2. 选取equals方法中用于比较的所有域,然后针对每个域的属性进行计算:
      (1) 如果是boolean值,则计算f ? 1:0
      (2) 如果是byte\char\short\int,则计算(int)f
      (3) 如果是long值,则计算(int)(f ^ (f >>> 32))
      (4) 如果是float值,则计算Float.floatToIntBits(f)
      (5) 如果是double值,则计算Double.doubleToLongBits(f),然后返回的结果是long,再用规则(3)去处理long,得到int
  3. public String toString()返回对该对象字符串的表示.
    Object默认实现的是返回对象名称和hashCode以16进制无符号形式表示

  4. protected native Object clone() throws CloneNotSupportedException;拷贝当前对象
    需要注意的地方:

    1. 被protected修饰,若希望被外部类调用则需要覆写时将访问权限修改成public

    2. 使用此方法则必须实现java.lang.Cloneable接口

    3. Object默认实现的是浅拷贝(浅拷贝:子对象只是复制引用),若希望实现深拷贝可以使用序列化

      /**
      * 测试Object的clone()方法
      * clone()方法是浅拷贝方法
      */
      public void testCloneMethod(){
      try {
      ObjectTest objectClone = (ObjectTest)this.clone();
      System.out.println("\n=clone()方法测试(浅拷贝)=");
      System.out.println("\t对象内其他对象引用是否相同:" + (this.user == objectClone.user));
      } catch (CloneNotSupportedException e) {
      // 必须实现java.lang.Cloneable接口,否则会抛出CloneNotSupportedException异常
      e.printStackTrace();
      }
      }

      /**
      * 使用序列化实现深拷贝
      * 需要实现Serializable接口
      */
      public ObjectTest deepClone()throws IOException,ClassNotFoundException{
      System.out.println("\n=深拷贝实现=");
      // 对象写入流
      ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
      ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
      objectOutputStream.writeObject(this);
      // 从流里读出对象
      ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
      ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
      ObjectTest objectTest = (ObjectTest)objectInputStream.readObject();
      System.out.println("\t对象内其他对象引用是否相同:" + (this.user == objectTest.user));
      return objectTest;
      }

      out:
      =clone()方法测试(浅拷贝)=
      对象内其他对象引用是否相同:true

      =深拷贝实现=
      对象内其他对象引用是否相同:false

参考:

  1. 互联网其它博客内容
  2. JavaSE文档
posted @ 2016-01-31 18:17  Zhang.Hao  阅读(128)  评论(0)    收藏  举报