Java rewrite serial 57-equals, finalize methods, hashCode method

I, on the java language, how to compare whether two strings are the same

1. double equal sign can not be used to compare if the two strings are equal, should be compared using the equals method, as in the example

 

Package com.bjpowernode.java_learning; 


public  class D57_1_ { 

  public  static  void main (String [] args) { 

    String S1 = new new String ( "the ABC" ); 

    String S2 = new new String ( "the ABC" ); 

    System.out.println (S1 == S2); // to false, this is because the comparison of two objects, the object is the address comparison 

    System.out.println (s1.equals (S2)); // the equals method of comparing strings is which value 

   

  } 

}

2. rewriting can be achieved if the class equals internally consistent determination target effects, rather than using the class action original address comparison

 

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 is more out of the false, because the comparison is the memory address of the two objects, but this is clearly not in line with our expectations 

    // is the fact that we want to compare the contents of an object which is not consistent. So we rewrite 

   

  } 

} 

class Star56 { 

  int ID; 

  String name; 

  public Star56 ( int ID, String name) { 

    the this .id = ID; 

    the this .name = name; 

  } 

  public  Boolean the equals (Object obj) { 

    IF (obj the instanceof Star56) { // casts must first determines both whether there is inheritance 

      Star56 S = (Star56) obj; 

      iF (== s.id the this .id && s.name.equals ( the this.name)) {

        return true;

      }

    }

    return false;

  }

}

​

Two, finalize methods, hashCode method

Each object has a method 1.finalize java

2.finalize method does not require programmers to call, called by the system

3.java objects if there are no more references to point to it, then the java object becomes garbage data, wait for garbage collected, the garbage collector finalize method that will automatically call the object before recycling the java object.

the object is to finalize ah soon be recovered, for example: the need to release resources, may be released in the process.

 

Package com.bjpowernode.java_learning; 


public  class D57_2_MethodOfFinalize { 

  public  static  void main (String [] args) { 

    Person57 P1 = new new Person57 (); 

    P1 = null ; // no references to it, waiting to be recovered 

    // programmers only to "recommend" the garbage collector garbage 

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

   

    // use hashcode method returns the hash value of the object, the object is to get the memory address of java through the int hash algorithm value type 

    Person57 P2 = new new Person57 (); 

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

   

    // use clone () method, may be a copy of the object, the original object to prevent damage to the contents of the 

  }

} 

Class Person57 { 

  // rewrite fianlize method Object methods 

  public  void fianlize () throws Throwable { // As for why so written, temporarily do not know, because the source code was written, we rewrite the function body on the line 

    System.out.println ( the this + "is soon recovered" ); 

    // can override finalize method such as re-written objects which specify a reference, in order to ease, the object to be garbage collected 

  } 

}

 

Third, the 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_44630050 (Xi Jun Jun Moods do not know - Rui)

3. Park blog: https: //www.cnblogs.com/ruigege0000/

4. Welcomes the focus on micro-channel public number: Fourier transform public personal number, only for learning exchanges, backstage reply "gifts" to get big data learning materials

 

 

Guess you like

Origin www.cnblogs.com/ruigege0000/p/11980490.html