HashCode以及深浅克隆的简单理解

76 阅读2分钟

1.HashCode

哈希码是用来代表对象的基本特征,一般是由32位二进制构成( int 保存)

hashcode有三种不同的算法:

  • object类:通过对象引用地址计算得到一个整型数值
  • 基本封装类(Integer):保存了里面对应的数值
  • String类:通过字符串本身的内容计算得到一个整型数值
 public static void main(String[] args) {
         //属于Object类的hashcode
         Demo1 d1=new Demo1();
         System.out.println("d1:"+d1.hashCode());
         Demo1 d2=new Demo1();
         Demo1 d3=d1;
         System.out.println("d2:"+d2.hashCode());
         System.out.println("d3:"+d3.hashCode());
 ​
         //Integer类的hashCode
         Integer num1=1000;
         Integer num2=new Integer(100000);
         Double d=100.0;
         Float f=15.5f;
         System.out.println("d:"+d.hashCode());
         System.out.println("f:"+f.hashCode());
         System.out.println("num1:"+num1.hashCode());
         System.out.println("num2:"+num2.hashCode());
         //String类hashcode
         String s1="java";
         String s2=new String("java");
         System.out.println("s1:"+s1.hashCode());
         System.out.println("s2:"+s2.hashCode());
     }

2.深浅克隆(深浅拷贝)

java有几种创建对象的方式?

这四种方式哪些情况使用构造方法?

1.克隆:在堆内存 将已经存在的对象拷贝一份

2.反序列化:通过本地文件(字节序列) 生成一个java中的对象 存储到堆内存中

  • 深克隆和浅克隆区别? ---面试题

    克隆:通过实现一个Cloneable接口 用于表示支持克隆,重写clone()

    • 浅克隆:只能针对于基本类型和String类型进行克隆 如果属性中存在引用类型 则不能克隆 只相当于把引用地址进行复制
    • 深克隆:不仅基本类型和String类型可以克隆 如果属性中存在引用类型(引用类型可能还会无限包含引用类型) 都可以克隆
     //浅克隆
     public class Demo2 {
         public static void main(String[] args) throws CloneNotSupportedException {
             A a=new A();
             a.b=new B();
             A a2=(A)a.clone();
             System.out.println(a); //A@4554617c
             System.out.println(a2); //A@74a14482
             a.id=100;
             a.name="张三";
             //引用类型不会克隆
             a.b.id=1000;
             a.b.name="王五";
             System.out.println(a.id+" "+a.name+" "+a.b+" "+a.b.id+" "+a.b.name);
             System.out.println(a2.id+" "+a2.name+" "+a2.b+" "+a2.b.id+" "+a2.b.name);
         }
     }
     //浅克隆
     class A implements Cloneable{
         Integer id;
         String name;
         B b;
         protected Object clone() throws CloneNotSupportedException {
             return super.clone();
         }
     }
     class B{
         Integer id;
         String name;
     }
    
     //深克隆
     public class Demo2 {
         public static void main(String[] args) throws CloneNotSupportedException {
     //        A a=new A();
     //        a.b=new B();
     //        A a2=(A)a.clone();
     //        System.out.println(a); //A@4554617c
     //        System.out.println(a2); //A@74a14482
     //        a.id=100;
     //        a.name="张三";
     //        //引用类型不会克隆
     //        a.b.id=1000;
     //        a.b.name="王五";
     //        System.out.println(a.id+" "+a.name+" "+a.b+" "+a.b.id+" "+a.b.name);
     //        System.out.println(a2.id+" "+a2.name+" "+a2.b+" "+a2.b.id+" "+a2.b.name);
     ​
             A a=new A();
             a.b=new B();
             a.b.c=new C();
             A a2=(A)a.clone();
             System.out.println(a+" "+a.b+" "+a.b.c);
             //结果如果 三个引用类型 地址都不同 表示深克隆成功
             System.out.println(a2+" "+a2.b+" "+a2.b.c);
         }
     }
     //深克隆
     class A implements Cloneable{
         Integer id;
         String name;
         B b;
         protected Object clone() throws CloneNotSupportedException {
             A a=(A)super.clone();
             a.b=(B)b.clone();
             return a;
         }
     }
     class B implements Cloneable{
         Integer id;
         String name;
         C c;
         protected Object clone() throws CloneNotSupportedException {
             B b=(B)super.clone();
             b.c=(C)c.clone();
             return b;
         }
     }
     class C implements Cloneable{
         @Override
         protected Object clone() throws CloneNotSupportedException {
             return super.clone();
         }
     }