Java serialization 57 equals rewrite, finalize method, hashCode method

Keywords: Java github Big Data

I. how to compare the consistency of two strings in java

1. You can't use double equal signs to compare whether two strings are equal. You should use the equals method to compare, for example

 

package com.bjpowernode.java_learning;

​

public class D57_1_ {

  public static void main(String[] args){

    String s1 = new String("ABC");

    String s2 = new String("ABC");

    System.out.println(s1 == s2);//false,This is because the two objects are compared, and the address of the object is compared

    System.out.println(s1.equals(s2));//equals Method is the value in the comparison string

   

  }

}

2. Overriding the equals of the class can achieve the effect of judging whether the objects are consistent or not, rather than using the original address comparison of the class

 

package com.bjpowernode.java_learning;

​

public class D56_2_equalsMethodAnalysis {

  public static void main(String[] args) {

    Object o1 = new Object();

    Object o2 = new Object();

    boolean b1 = o1.equals(o2);

    System.out.println(b1);

    Star56 s1 = new Star56(100,"xiaoming");

    Star56 s2 = new Star56(100,"xiaoming");

    System.out.println(s1.equals(s2));

    //This compares false,Because the memory addresses of two objects are compared, but this obviously does not meet our expectations

    //In fact, what we want to compare is whether the contents of the two objects are consistent. So we rewrite

   

  }

}

class Star56{

  int id;

  String name;

  public Star56(int id,String name) {

    this.id = id;

    this.name = name;

  }

  public boolean equals(Object obj) {

    if(obj instanceof Star56) {//To cast a mandatory type, you must first make a judgment whether there is an inheritance relationship between the two

      Star56 s = (Star56)obj;

      if (s.id == this.id && s.name.equals(this.name)) {

        return true;

      }

    }

    return false;

  }

}

​

II. finalize method and hashCode method

1.finalize method every java object has

2. The finalize method does not need to be called by the programmer, but is called by the system

3. If there is no more reference to the java object, the java object becomes garbage data and waits for the garbage collector to recycle. Before the garbage collector recycles the java object, it will automatically call the finalize method of the object.

The finalize method is about to be recycled. For example, if you need to release resources, you can release them in the method.

 

package com.bjpowernode.java_learning;

​

public class D57_2_MethodOfFinalize {

  public static void main(String[] args) {

    Person57 p1 = new Person57();

    p1 = null;//No reference is pointing to it, waiting to be recycled

    //Programmers can only "suggest" garbage collectors

    System.gc();//This is the system

   

    //Use hashcode Method, which returns the hash value of the object, java The memory address of the object is obtained by hash algorithm int Type value

    Person57 p2 = new Person57();

    System.out.println(p2.hashCode());

   

    //Use clone()Method, you can copy an object to prevent the original object content from being destroyed

  }

}

class Person57{

  //Rewrite Object Method in fianlize Method

  public void fianlize() throws Throwable{//As for why it's written like this, I don't need to know for the moment, because it's written like this in the source code. We can rewrite the function body in it

    System.out.println(this + "It's going to be recycled soon");

    //Can rewrite finalize Methods, such as rewriting it to the object to specify a reference, so as to alleviate the problem that the object will be recycled by the garbage collector

  }

}

 

Source code:

D57_1_CompareString.java

D57_2_MethodOfFinalize.java

Address:

https://github.com/ruigege66/Java/blob/master/D57_1_CompareString.java

https://github.com/ruigege66/Java/blob/master/D57_2_MethodOfFinalize.java

2.CSDN: https://blog.csdn.net/weixin_

3. Blog Park: https://www.cnblogs.com/ruige0000/

4. welcome to pay attention to WeChat public number: Fourier transform, personal public number, only for learning exchanges, background reply "gift package", access to big data learning materials.

 

Posted by kristolklp on Wed, 04 Dec 2019 11:55:17 -0800