JavaSE-Common Class-Object Class (12)

One, the introduction of the Object class
  1. Object class: Located in the java.lang package, it is the root class and super class of the inheritance relationship, and the parent class of all classes (direct parent class or indirect parent class);

  2. Object type references can be used to store any type of object;

  3. Methods are defined in the Object class, and all classes can be used directly.

2. Common methods in the Object class:
  1. getClass(): Returns the type of object actually stored in the reference.

    Development of practical applications: used to determine whether the object types actually stored in the two references are the same,

    ​ Consistent-true; inconsistent-false.

    class Animal{
          
          }
    class Dog extends Animal{
          
          }
    class Cat extends Animal{
          
          }
    public class TestAnimal{
          
          
        public static void main(String[] args){
          
          
            Animal a1 = new Cat();
         	Animal a2 = new Dog();
          	System.out.println(a1.getClass() == a2.getClass());
        }
    }
  2. int hashCode(): Returns the integer hash code value of the object. The source of the hash code value of the object is

    ​ Hexadecimal address, the result of converting a decimal integer.

    Note: Since different objects have different addresses in the heap space, all different objects

    ​ Has different hash code values.

  3. String toString(): Returns the realization form of the object string.

    (1) Function: It is convenient to display the information of the object, and the subclass usually covers the toString method

    (2) The principle of toString method coverage is: usually all attributes are spliced ​​into a string.

    ​ The result is returned.

    public class TestStudent{
          
          
        public static void main(String[] args){
          
          
            Student s = new Student("liucy",18,99.0);
            System.out.println(s);// 默认调用toString方法
        }
    }
    class Student{
          
          
        private String name;
        private int age;
        private double score;
        public Student(){
          
          }
        public Student(String name,int age,double score){
          
          
            this.name=name;
            this.age=age;
            this.score=score;
        }
        // get/set
        public String toString(){
          
          
            return "name:"+name+",age:"+age+",score:"+score;
        }
    }

    Note: The difference between get method and toString method:

    a. The get method returns the information of a single attribute of the object, and does not change the corresponding data of the attribute

    ​ Type

    b. The toString method gets the information of all attributes, and splices all attributes into one

    ​ The result of the String type is returned.

  4. boolean equals(Object o): used to compare whether the object content is the same.

    == Application

    (1) If the two ends of == are variables of basic data type, then judge whether the values ​​in the variables are the same

    ​ If both ends of == are references, it is judged whether the addresses stored in the references are the same.

    (2) Practical application in development: usually used to determine whether two references point to the same object.

    ​ Expression: reference name 1 == reference name 2

    ​ a. The result is true, which means that the two references point to the same object;

    ​ b. The result is false, which means that the two references point to different objects.

    Application of equals method:

    (1) The implementation principle of equals method in the parent class Object class is as follows:

    public boolean equals(Object obj) {
          
          
    	return (this == obj);
    }

    Note: The equals method in the parent class is used to compare whether two references are the same object, but during development, the equals method is usually used to compare the content of the object, so the equals method in the parent class is not enough to meet the needs of the subclass, so you need to override The equals method in the parent class Object.

    (2) The principle of equals method coverage:

    public boolean equals(Object obj){
          
          
    	// 1. 自反性:判断当前对象和要比较的对象是否为同一个对象 :        //this(当前对象) 和  obj(要比较对象) 
    	if(this == obj){
          
          
    		return true;
    	}
    	// 2. 判断 obj是否 空
    	if(obj == null){
          
          
    		return false;
    	}	
    	// 3. 判断 两个引用中实际存储的对象类型是否一致
    	if(this.getClass() != obj.getClass()){
          
          
    		return false;
    	}
        
    	// 4. 强制类型转换: 为一一比较对象属性做准备
    	Student s = (Student)obj;
    		
    	// 5. 将对象中属性一一进行比较:基本数据类型的属性:==  ; 引用类型:equals比较
    	if(this.age == s.age && this.score == s.score && this.name.equals(s.name)){
          
          
    		return true;
    	}else{
          
          
    		return false;
    	}
    }

    Interview focus: The difference between == and equals method.

    (1) == Both ends are basic data types, compare whether the values ​​in the variables are the same

    ​ == Both ends are reference type variables, compare whether the reference storage address is the same

    (2) Equals method: The method in the Object class is used to implement (==) implementation, compare the object

    ​ address; but in actual development, the equals method needs to be overwritten to compare objects

    Whether the contents of ​ are the same.

  5. finalize(): A method that is automatically called when the JVM performs garbage collection.

    (1) Garbage object: an object pointed to by no reference.

    Student s= new Student();

    s = null; // Set the object pointed to by s as a garbage object

    (2) Garbage collector: used for JVM to collect garbage objects. Referred to as gc / GC

    (3) The purpose of garbage collection: remove garbage objects, release space, and improve space utilization.

    (4) When the garbage collector collects garbage objects:

    a. Automatic recycling mechanism: When the JVM memory is exhausted and space can no longer be allocated for newly created objects, the garbage collector in the JVM will automatically recycle all garbage objects at one time. When the garbage collector recycles garbage objects, The finalize method is automatically called.

    b. Manual collection mechanism: Use System.gc(); Notify the garbage collector (GC) to perform garbage collection. If the GC is idle, then the garbage collection will be performed. If the GC is busy, it will not be collected temporarily

    Note: During development, no important code is written in the finalize method, and the execution node of the finalize method is uncontrollable.

Guess you like

Origin blog.csdn.net/Java_lover_zpark/article/details/105259729