Home  >  Article  >  Java  >  Java Object class method instance analysis

Java Object class method instance analysis

王林
王林forward
2023-04-18 17:49:051029browse

1.equals method

1.Introduction to the equals method in API

Java Object class method instance analysis

2.Comparison between == and equals

  • == You can judge both basic types and reference types. If you judge the basic type, you judge whether the values ​​are equal. If you judge the reference type, you judge whether the addresses are equal, that is, Determine whether they are the same object.

  • The equals method is a method in the Object class. It can only determine the reference type. The default is to determine whether the addresses are equal. This method is often overridden in subclasses. After rewriting, it is used Determine whether the contents are equal

We can look at the JDK source code of the equals method in Object, String and Integer.

Object o = new Object();
o.equals(1);//Object类中的equals方法
System.out.println("hello".equals("hello"));//String中的equals方法
Integer integer = new Integer(5);
System.out.println(integer.equals(5));//Integer中的equals方法
//将光标放在equals方法上,按Ctrl同时按鼠标左键,就可跳转到String和Integer中的equals方法的JDK源码。
//Object类中的equals方法
public boolean equals(Object obj) {
        return (this == obj);//判断是不是当前对象
}
//String类中重写的equals方法
public boolean equals(Object anObject) {
        if (this == anObject) {
            return true;//首先判断是不是当前对象
        }
        if (anObject instanceof String) {//判断当前类型是不是字符串类型
            String anotherString = (String)anObject;//向下转型
            int n = value.length;//保存传入字符串的长度
            if (n == anotherString.value.length) {
                char v1[] = value;
                char v2[] = anotherString.value;
                int i = 0;
                while (n-- != 0) {
                    if (v1[i] != v2[i])//一个一个字符比较
                        return false;
                    i++;
                }
                return true;
            }
        }
        return false;
    }
 //Integer类中重写的equals方法
public boolean equals(Object obj) {
        if (obj instanceof Integer) {//判断当前类型是不是Integer类型
            return value == ((Integer)obj).intValue();//向下转型
        }
        return false;
    }

2.hashCode method

1.Introduction to hashCode method in API

Java Object class method instance analysis

2.Improve the efficiency of containers with hash structures efficiency.

3. If two references point to the same object, the hash value is the same. If they point to different objects, the hash value is different.

4. The hash value is mainly based on the address, but the hash value cannot be equivalent to the address.

3.toString method

1.Introduction to toString method in API

Java Object class method instance analysis

2.Default return: full class name (package name class name) @hex value of the hash value.

3. The toString method is often overridden to return object properties.

4. When the object is output directly, the toString method will be called by default.

//JDK中的toString方法源码
public String toString() {
        return getClass().getName() + "@" + Integer.toHexString(hashCode());
    //返回全类名(包名+类名)+@+哈希值的十六进制
    }
//重写toString方法
 @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", gender=" + gender +
                '}';
    }//往往重写toString方法,用于返回对象属性。

4.finalize method

1.Introduction to the finalize method in API

Java Object class method instance analysis

2.When the object is recycled, the system automatically calls it The finalize method of this object. Subclasses can override this method and perform some operations to release resources.

3. When an object has no reference, the jvm will use the garbage collection mechanism to destroy the object. The finalize method will be called before destroying the object.

4. The invocation of the garbage collection mechanism is determined by the system's GC algorithm, and can also be actively triggered through System.gc().

The above is the detailed content of Java Object class method instance analysis. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete