项目代码
https://github.com/yinhai1114/Java_Learning_Code/tree/main/IDEA_Chapter08/src/com/yinhai/object_
一、equal方法
==和equal的对比
1.==既可以判断基本类型,又可以判断引用类型,返回boolean值
2.== 如果判断基本类型,判断的值是否相等 int i = 10;double d = 10.0 ,i == d
3.==如果判断引用类型,判断的是地址是否相等,即判断是不是同一个对象
public class Equals01 {
public static void main(String[] args) {
A a = new A();
A b = a;
A c = a;
System.out.println(a == c);//ture or false ? ture,because a and c reference to same object
System.out.println(b == c);//same above
B d = a;//transition up
System.out.println(d == a);//same above,have same as run type so point to same address
}
}
class A extends B{
}
class B{
}
4.equals 是Object类中的方法,只能判断引用类型
如何看Jdk源码
Idea 如何查看 Jdk源码
- 一般来说IDEA配置好JDK以后,jdk的源码也就自动配置好了
- 如果没有的话点击菜单File --> Project Structure --> SDKs --> Sourcepath然后点击右侧绿色的加号
- 在需要查看某个方法源码时,将光标放在该方法,输入ctrl + b即可
或者在方法上 点击右键->go to -> Declaration or ...
5.默认判断的是地址是否相同,子类中往往重写该方法,用于判断内容是否相同,比如Interger,String
//original code of equals method
public boolean equals(Object anObject) {//judge adress
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 also override equals method
//here they judge whether two object have same value
public boolean equals(Object obj) {
if (obj instanceof Integer) {
return value == ((Integer)obj).intValue();
}
return false;
}
二、equals方法练习
1.equals方法重写,应用实例,判断两个对象的内容是否相等,如果两个对象的各个属性值都一样,则返回true,反之返回false
package com.yinhai.object_;
public class EqualsExercise {
public static void main(String[] args) {
Person person = new Person();
Person person1 = new Person();
System.out.println(person.equals(person1));
}
}
class Person {
private String name;
private int age;
private char gender;
public boolean equals(Object b) {
if (this == b) {
return true;
}
if (b instanceof Person) {
Person p = (Person) b;
// why we need transition down? because we need to getting unicon parameter of b;
return this.name.equals(p.name) && this.age == p.age && this.gender == p.gender;
//why this.name.equals(p.name) this statement running not method of class Person?
//because want to use equals method in current method,that statement is equals(p);
//but this running "String" of method used this.name.equals()
}
return false;
}
}
特别注意,在这里方法的equals()表示class Person本类内的方法,而不会递归调用的原因是this.name.equals表示调用的是String内的方法
2. 判断
package com.yinhai.object_.equalsexercise02;
public class EqualsExercise02 {
public static void main(String[] args) {
Person p1 = new Person();
p1.name = "xiaowang";
Person p2 = new Person();
p2.name = "xiaowang";
System.out.println(p1 == p2);//f different address
System.out.println(p1.name.equals(p2.name));//t use String of method
System.out.println(p1.equals(p2));//f different address
String s1 = new String("asdf");
String s2 = new String("asdf");
System.out.println(s1.equals(s2));//t same above
System.out.println(s1 == s2);//f
}
}
class Person{
public String name;
}
3.
package com.yinhai.object_.equalsexercise03;
public class EqualsExercise03 {
public static void main(String[] args) {
int it = 65;
float fl = 65.0f;
System.out.println(it == fl);//t
char ch1 = 'A';
char ch2 = 12;
System.out.println(it == ch1);//t
System.out.println(12 == ch2);//t
String str1 = new String("hello");
String str2 = new String ("hello");
System.out.println(str1 == str2);//f
System.out.println(str1.equals(str2));//t
// System.out.println("hello" == new java.sql.Data());
}
}
三、hashCode方法
1.提高具有哈希结构的容器的效率
2.两个引用,如果指向的是同一个对象,则哈希值肯定是一样的
3.两个引用,如果指向的是不同的对象,则哈希值是不一样的
4.hashCode是主要是根据地址来的,但不能完全将哈希值等价于地址
因为java是在虚拟机上跑的
5.案例
package com.yinhai.object_.hashcode_.hashcode01;
public class HashCode01 {
public static void main(String[] args) {
AA aa = new AA();
AA aa1 = new AA();
AA aa2 = aa;
System.out.println("aa.hashcode " + aa.hashCode());
System.out.println("aa1.hashcode " + aa1.hashCode());
System.out.println("aa2.hashcode " + aa2.hashCode());
}
}
class AA{
}
6.后面在集合中hashCode往往在使用中会被重写
四、toString方法
1.基本介绍
默认返回:全类名 + @ + 哈希值的十六进制 【查看Object的toString方法】
子类往往重写toString方法,用于返回对象的属性信息
2.重写toString方法
3.当直接输出一个对象时,toString会被默认调用monster.toString()
package com.yinhai.object_.tostring_;
public class ToString {
public static void main(String[] args) {
// public String toString() {
// return getClass().getName() + "@" + Integer.toHexString(hashCode());
// }return getClass().getName()//this full class name
// And then the next one is hashcode
Monster monster = new Monster("little goblin", "mountain patrol", 3000);
System.out.println(monster.toString() + " hashcode=" + monster.hashCode());
//have not override so this statement call method of the base class
System.out.println(monster);
//3.toString method is called by default,when direct output
//remember,when comment out toString of Monster,it will to call in method of Object
}
}
class Monster{
private String name;
private String job;
private double sal;
public Monster(String name, String job, double sal) {
this.name = name;
this.job = job;
this.sal = sal;
}
@Override
public String toString() {
return "Monster{" +
"name='" + name + '\'' +
", job='" + job + '\'' +
", sal=" + sal +
'}';//in general,default output parameter of object
}
}
}
当注释掉已经重写的toString方法时 会调用父类的方法
五、finalize方法
1.当对象被回收时,系统自动调用该对象的finalize方法(该方法默认什么都不做)。子类可以重写该方法
2.什么时候被回收:当某个对象没有任何引用时,jvm就认为这个对象是一个垃圾对象,就会使用垃圾回收机制来销毁该对象,在销毁该对象前,会先调用finalize方法
3.垃圾回收机制的调用是由系统来决定的(即有自己的GC算法),也可以通过System.gc()来主动处罚垃圾回收机制
package com.yinhai.object_.finalize_;
public class Finalize_ {
public static void main(String[] args) {
Car bmw = new Car("BMW");
bmw = null;
//currently,object of Car is garbage,
// garbage collector will recycling object
// the finalize method is called before recycling the object
//so we can override this finalize method.
// inside Object class,the finalize method does nothing by default
System.gc();//note,possibly this is not immediately called,maybe be occupied with System or busy
//can look that finalize cant running , why?
//because they have unique algorithm inside system,does not immediately run
//so we can use System.GC() to running garbage collector mechanism.
//but
System.out.println("Program exit...");
}
}
class Car{
private String name;
public Car(String name){
this.name=name;
}
@Override//default handling by method of Object
protected void finalize() throws Throwable {
System.out.println("we recycling car" + name);
System.out.println("release space...");
}
}
不会卡着程序不走,执行完之后会返回