Object类
概述
概念
Object是类层次结构的根类.每个类都使用Object作为超类.所有对象(包括数组)都实现这个类的方法.
随意定义一个类型,不手动显式定义其父类,那么这个类的父类就是Object类.
Object类: 是所有类的父类 ,Object类没有父类 是所有类的爸爸
特点
1. Object类型的引用 可以指向任何类的对象
2. Object类中定义的所有的非私有的属性和方法 在所有的类中都拥有使用权
3. 当定义一个类的时候,没有继承任何的类,默认这个类 继承Object
4、Object类如果不是一个类的直接父类,必定是这个类的间接父类
构造方法
语法
Object()
用途
1.创建Object类的对象的方法.
2.子类访问,所有子类都会直接或者间接的访问到这个顶层父类的构造方法.
常用方法
toString()
作用
返回对象的字符串形式
方法实现
输出
getClass().getName() + '@' + Integer.toHexString(hashCode())
过程
(1)getClass().getName() 表示类的完全限定名.
(2)hashCode() 表示根据内存地址通过哈希算法生成的哈希码值.
(3)Integer.toHexString() 表示无符号十六进制的字符串表示形式.
过程详解
输出内容:getClass().getName() + '@' + Integer.toHexString(hashCode())
(1)getClass.getName(): 作用:获取对象的全类名
1)getClass()
作用:返回此 Object 的运行时类。
获取的是类的字节码对象 ,就是 Class 类的对象,输出结果最前面会带有 class.
对象中存储了 关于 xx类中所有的属性名 、 类名、 包名 、 构造方法名、 构造方法 .....
例:class com.study.day0416.Student
2)getName()
必须与getClass()连用,不可单独使用
getClass.getName(): 作用:获取对象的全类名、带包结构的类名. 即包名 + 类名组成的字符串(会消除最前方的class)
例: com.study.day0416.Student
(2)hashCode()
作用:返回该对象的哈希码值。
哈希值: 将对象的地址进行哈希算法的计算得到的一个整数值 这个值叫做哈希值
hashCode方法的实现不是Java语言实现的 是一个本地方法
(3)Integer.toHexString(int a)
作用:将一个10进制的整数 转换为16进制的数
特点
1、在实际开发中一般遵从重写原则
2、直接打印对象的引用,就是打印的是对象调用toString方法后返回的字符串的值。

案例
重写前
package com.study.day14;
public class Demo2 {
public static void main(String[] args) {
//创建一个学生对象
Student s1 = new Student("老王",16);
System.out.println(s1);//com.study.day14.Student @ 1b6d3586
System.out.println(s1.toString());
// getClass().getName() + '@' + Integer.toHexString(hashCode())
String s = s1.toString();
System.out.println(s);
//com.ujiuye.day14.Student ---> 对象的全类名 带包结构的类名
Class aClass = s1.getClass();//获取的是 student类的 字节码对象 就是 Class 类 的对象
//aClass 对象中 存储了 关于 学生类中所有的属性名 类名 包名 构造方法名 构造方法 .....
String name = aClass.getName();//获取 包名 + 类名组成的字符串
System.out.println(name);//全类名
// Integer.toHexString(hashCode())
//hashCode() ---> 将对象的地址进行哈希算法的计算得到的一个整数值 这个值叫做哈希值 hashCode方法的实现不是Java语言实现的 是一个本地方法
int hash = s1.hashCode();//int类型值 哈希值 -2147483648 --- 2147483647
System.out.println(hash);//460141958
//Integer.toHexString(int a)
//将一个10进制的整数 转换为16进制的数
String s2 = Integer.toHexString(460141958);
System.out.println(s2);//1b6d3586
String result = name+"@"+s2;
System.out.println(result);//com.study.day14.Student@1b6d3586
}
}
class Student{
String name;
int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
}
重写后
package com.study.day14;
public class Demo2 {
public static void main(String[] args) {
//创建一个学生对象
Student s1 = new Student("老王",16);
System.out.println(s1);
}
}
class Student{
String name;
int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
//重写toString方法
// @Override
// public String toString() {
// return "姓名:"+this.name+" 年龄:"+this.age;
// }
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
equals()
作用
判断两个对象是否相等
特点
1、在实际开发中重写原则
2、比较两个对象的所有属性/部分属性 是否全部相同
步骤:
(1)判断两个引用是否相同
(2)判断obj是否为空
(3)判断两个引用的类型是否相同
(4)向下转型
(5)判断属性是否相同
== 和 equals 方法的区别
简单数据类型
1. == 可以判断两个简单数据类型的值是否相等
2. equals方法不能用于简单数据类型
引用数据类型
1. == 可以判断两个引用中的地址是否相同
2. equals方法在重写之前和 == 一样,重写之后判断的是两个对象的内容属性是否相同
案例
//案例1:
package com.study.day14;
public class Demo2 {
public static void main(String[] args) {
//创建学生对象
Student s1 = new Student("老王",15,100);
Student s2 = s1;
//Student s2 = new Student("老王",15,100);
// Student s2 = null;
//Dog s2 = new Dog();
//System.out.println(s1 == s2);//判断的是s1中的地址 和 s2中的地址是否相等
// == 在引用数据类型中 不能判断两个对象是否是同一个对象
System.out.println(s1.equals(s2));//使用的是 Object 继承下来的 你没有给我标准
}
}
class Student{
String name;
int age;
int code;//学号 唯一 不会重复
public Student() {
}
public Student(String name, int age,int code) {
this.name = name;
this.age = age;
this.code = code;
}
//重写toString方法
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
//重写equals 方法
@Override
public boolean equals(Object o) {
// 1判断两个引用的地址是否相同
if (this == o){
return true;
}
//2 判断 o 是否为空
if (o == null){
return false;
}
//3 判断这两个引用中存放的对象是否是同一个类型
if (this.getClass() != o.getClass()){
return false;
}
// 4 向下转型
Student s = (Student) o;
//this s
if (this.code == s.code){
return true;
}else{
return false;
}
}
}
class Dog{
}
//案例2:
package com.study.day14;
public class Demo2 {
public static void main(String[] args) {
//创建学生对象
Student s1 = new Student("老王",15,100);
Student s2 = s1;
System.out.println(s1.equals(s2));//使用的是 Object 继承下来的 你没有给我标准
}
}
class Student{
String name;
int age;
int code;//学号 唯一 不会重复
public Student() {
}
public Student(String name, int age,int code) {
this.name = name;
this.age = age;
this.code = code;
}
//重写toString方法
// @Override
// public String toString() {
// return "姓名:"+this.name+" 年龄:"+this.age;
// }
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
//重写equals 方法
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return age == student.age && code == student.code && name.equals(student.name);
}
}
finalize()
作用
当垃圾回收器确定不存在对该对象的更多引用时,由对象的垃圾回收器调用此方法。
一般语法:
Object的子类可重写此方法
案例
package com.study.day14;
public class Demo5 {
public static void main(String[] args) {
Test test = new Test();//不是垃圾
test = null;//test不指向 对象 new Test()没有引用指向它了 就成为垃圾 到达一定的程度 垃圾回收器会进行回收
//通知进行垃圾回收
System.gc();
//当垃圾被回收了 调用finalize方法
// for (int i = 0; i < 10000000; i++) {
// new Test();//100个垃圾
// }
}
}
class Test extends Object{
//重写
@Override
protected void finalize() throws Throwable {
System.out.println("我被调用了 垃圾被回收了");
super.finalize();
}
}