Object类
·超类、基类,所有类的直接或间接父类,位于继承树最顶层
·任何类,如果没有书写extends显示继承某个类,都默认直接继承Object类,否则为间接继承
·Object类中所定义的方法,是所有对象都具备的方法
·Object类型可以存储任何对象
·作为参数,可以接受任何对象
·作为返回值,可返回任何对象
getClass()方法
·public final Class<?> getClass(){}
·返回引用中存储的实际对象类型
·应用:通常用于判断两个引用中实际存储对象类型是否一致
public class TestStudent {
public static void main(String[] args) {
Student s1 = new Student("aaa",111);
Student s2 = new Student("bbb ",222);
//判断S1和S2是不是同一个类型
Class class1 = s1.getClass();
Class class2 = s2.getClass();
if (class1==class2){
System.out.println("s1和s2是同一个类型");
}
else {
System.out.println("no");
}
}
}
public class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
hashCode()方法
·public int hashCode(){}
·返回该对象的哈希码值
·哈希值根据对象的地址或字符串或数字使用hash算法计算出来的int类型的数值
·一般情况下相同对象返回相同的哈希码
public class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
public class TesthashCode {
public static void main(String[] args) {
Student s1 = new Student("cc",333);
Student s2 = new Student("dd",444);
System.out.println(s1.hashCode());//764977973
System.out.println(s2.hashCode());//381259350
Student s3 = s1;
System.out.println(s3.hashCode());//764977973
}
}
toString()方法
·public String toString(){}
·返回对象的字符串表示(表现形式)
·可以根据程序需求覆盖方法,如:展示对象各个属性值
public class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
//重写
@Override
public String toString() {
return name+age;
}
}
public class TesttoString {
public static void main(String[] args) {
Student s1 = new Student("aaa",111);
Student s2 = new Student("bbb",222);
System.out.println(s1.toString());//com.common.Object.toString.Student@2d98a335 哈希值
System.out.println(s2.toString());//com.common.Object.toString.Student@16b98e56
}
}
equals()方法
·public boolean equals(Object ogj){}
·默认实现为(this==obj),比较两个对象的地址是否相同。
·可进行覆盖,比较两个对象的内容是否相同。
equals()方法重写步骤:
·比较两个引用是否指向同一个对象
·判断obj是否为null
·判断两个引用指向的实际对象类型是否一致
·强制类型转换
·依次比较各个属性值是否相同
public class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public boolean equals(Object obj) {
//1、判断两个对象是否是同一个
if (this == obj) {
return true;
}
//2、判断obj是否为null
if (obj == null) {
return false;
}
//3、判断是否是同一个类型
// if (this.getClass()==obj.getClass()){}
//instanceof判断对象是否是某种类型
if (obj instanceof Student) {
//4、强制类型转换
Student s = (Student) obj;
//5、比较数据
if (this.name.equals(s.getName()) && this.age == s.getAge()) {
return true;
}
}
return false;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
public class TestEquals {
public static void main(String[] args) {
Student s1 = new Student("aaa",111);
Student s2 = new Student("aaa",111);
//equals方法判断两个对象是否相等
System.out.println(s1.equals(s2));//false未重写
}
}
finalize()方法
·当对象被判定为垃圾对象是,由jvm自动调用此方法,用以标记垃圾对象,进入回收队列。
·垃圾对象:没有有效引用此对象时,为垃圾对象。(没有任何引用指向他的对象)
·垃圾回收:由GC销毁垃圾对象,释放数据存储空间。
·自动回收机制:jvm的内存耗尽,一次性回收所有垃圾对象。
·手动回收机制:使用System.gc();通知jvm执行垃圾回收。
public class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
protected void finalize() throws Throwable {
System.out.println("对象被回收了");
}
}
public class Teatfinalize {
public static void main(String[] args) {
Student s1 =new Student("mdy",11);
Student s2 =new Student("mdy",11);
Student s3 =new Student("mdy",11);
Student s4 =new Student("mdy",11);
new Student("mdy",11);//回收
//回收垃圾
System.gc();
System.out.println("回收垃圾");
}
}