Java's Object class summary

Object class overview:

1.Object class is the root class of all Java classes.

2. If you do not use the extends keyword in the class declaration identified in the parent class,

The default parent class is java.lang.Object class.

Object class main structure:

 Operator ==

1. The basic types of compare value: as long as the two variables are equal, that is true.

2. Reference type comparison reference (point to the same object): Only point to the same object,

== operator returns true only.

3. When the operator == are compared, both sides of the symbol data types must be compatible (automatic conversion

Except for basic data types), or compiler errors.

equals () format: obj1.equals (obj2)

1. All classes inherit from Object, also won the equals () method can also be rewritten.

2. You can compare reference type, its role and "==" the same, whether the comparison point to the same object.

3. Special case: when the comparison equals () method of class File, String, Date and packaging

(Wrapper Class), it is more the type and content regardless of whether the reference is to the same object.

Reason: rewriting equals () method of class Object in those classes.

4. When using custom equals (), it can be rewritten. Used to compare two objects of "content" is all equal.

Rewrite equals () method principles:

1. Symmetry: If x.equals (y) returns is "true", then y.equals (x) should return a "true".

Reflexivity: x.equals (x) must return a "true".

2. transitive: if x.equals (y) returns a "true", and y.equals (z) returns yes "true",

So z.equals (x) should return a "true".

3. Consistency: If x.equals (y) returns is "true", as long as the x and y have the same content, whether you

Repeat x.equals (y) the number of times, returns are "true".

4. Under no circumstances, x.equals (null), always returns is "false".

5. any case, x.equals (and a different type of object x) always returns a "false".

== operator and equals () Contrast:

1. == operator may be more basic types may be compared with a reference type.

For basic type is the comparison value for reference types is to compare the memory address.

2.equals () which belongs to the class java.lang.Object method,

If the method is not overridden default operator ==.

Depends on the specific custom class 3. There is no override Object equals method to determine.

4. Typically, the equals method to rewrite the corresponding property class if the comparison are equal.

 toString () method Overview:

1.toString () methods defined in the class Object, the return value is of type String, it returns a reference to the class name and address.

2. String performing operation connected with other types of data, automatically calls toString () method

Date now=new Date();

System.out.println("now="+now); 相当于

System.out.println("now="+now.toString();

3 can be rewritten in a user-defined type toString () method

The String class overrides toString () method returns the value of the string.

s1="hello";

System.out.printIn (s 1); corresponds System.out.println (s1.toString ());

4. The basic types of data is converted into String type, called toString () method corresponding to the wrapper class

int a=10; System.out.println("a="+a);

Precautions:

1.public String toString () Returns a string representation of the object.

2. Typically toString method returns a "textually represents" character string of this object,

The result should be a concise but easy-to-read information expression, it is recommended that all subclasses override (cover) this method.

toString method 3.Object class returns a string name of the class (an object is an instance of the class),

at tag "@" and the hash code for this object unsigned hexadecimal representation of the composition.

即getClass().getName() + '@' + Integer.toHexString(hashCode())

4. The character string representation of the object: by a string, the reaction useful information object,

The value of a member variable toString overwrite the contents of the returned object.

. 1  public  class the ToString {
 2    public  static  void main (String [] args) {
 . 3      Object O = new new Object ();
 . 4      String S = o.toString ();
 . 5      System.out.println (S);   // Java. @ 4554617c lang.Object 
. 6  
. 7      imitate imitate = new new imitate ();
 . 8      // String S1 = imitate.imitateToString (O); 
. 9      System.out.println (imitate.toString ());
 10  
. 11      // output an object, It is output string toString method returns the object 
12     System.out.println (imitate);
 13 is  
14      // JVM calls the default object and the string concatenation method toString 
15      System.out.println ( "wangdao" + imitate);
 16    }
 . 17  }
 18 is  
. 19  class Imitate {
 20 is    int = 10 I ;
 21 is    int J = 20 is ;
 22 is    public String imitateToString (Object O) {
 23 is      return o.getClass () getName () + '@' +. Integer.toHexString (o.hashCode ());
 24    }
 25  
26 is    @override
 27    public String toString () {
28     return "Imitate{" +
29         "i=" + i +
30         ", j=" + j +
31         '}';
32   }
33 }

hashCode () Overview:

1.public int hashCode () Returns the hash code value for this object.

2. This method is supported in order to improve (e.g., a hash table provided by java.util.Hashtable) performance of the hash table.

3. hashCode method defined by class Object does return an integer different for different objects.

4. Object mapped to an integer, the internal address of the object into an integer achieved.

hashcode of the general contract:

1. During the Java application execution, when calling the hashCode method on the same object multiple times, it must consistently return the same integer,

Provided that the object information used in equals comparisons has not been modified.

To the same application of another one execution of an application from the integer need not remain consistent.

2. Where equals (Object) method, two objects are equal,

So these two objects each object calling the hashCode method must produce the same integer result.

3. If according equals (java.lang.Object) method, two objects are not equal,

Then calling the hashCode method on two objects do not require any object must produce distinct integer results.

. 1  public  class HashCodeDemo {
 2    public  static  void main (String [] args) {
 . 3      HashCodeDemo hashCodeDemo = new new HashCodeDemo ();
 . 4      // System.out.println (hashCodeDemo.hashCode ());
 . 5      // create an object 
. 6      HashCodeDemo = hashCodeDemo1 new new HashCodeDemo ();
 . 7      // indicating some other object (method EAQUALS reception parameter) 
 8      // (object calls equals method) whether the object is "equal to." 
. 9      hashCodeDemo.equals (hashCodeDemo1);
 10      A = A new new A (. 1 );
 . 11     A a1 = new A(1);
12     System.out.println(a.hashCode());
13     System.out.println(a1.hashCode());
14 
15     System.out.println("==================");
16     //第一次判断之后
17 
18     a1.i = 2;
19     System.out.println(a.hashCode());
20     System.out.println(a1.hashCode());
21   }
22 
23 }
24 
25 class A {
26   int i;
27   int j;
28   double k;
29   public A(int i) {
30     this.i = i;
31   }
32 
33   @Override
34   public int hashCode() {
35     int result;
36     long temp;
37     result = i;
38     result = 31 * result + j;
39     temp = Double.doubleToLongBits(k);
40     result = 31 * result + (int) (temp ^ (temp >>> 32));
41     return result;
42   }
43 }

Guess you like

Origin www.cnblogs.com/ZengBlogs/p/12172813.html