Object类
在java.lang包下
超类,所有对象都继承这个类的方法
getClass()方法
返回class类型
应用:判断两个引用中实际存储对象类型是否一致
package com.object.demo06; 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; } }
package com.object.demo06; public class Test { public static void main(String[] args) { Student s1 = new Student("aaa", 20); Student s2 = new Student("bbb", 22); //判断s1和s2是不是同一个类型 Class class1 = s1.getClass(); Class class2 = s2.getClass(); if (class1 == class2) { System.out.println("s1 is the same class"); } else { System.out.println("s1 is not the same class"); } } }
hashCode()方法
返回int类型
哈希值根据对象的地址或字符串或数字使用hash算法计算出来的int类型的数值
一般情况下相同对象返回相同哈希码
以上节课的代码为例
package com.object.demo06; public class Test { public static void main(String[] args) { Student s1 = new Student("aaa", 20); Student s2 = new Student("bbb", 22); //hashCode方法 System.out.println(s1.hashCode()); //开辟第一块空间 System.out.println(s2.hashCode()); //开辟第二块空间 Student s3 = s1; System.out.println(s3.hashCode()); //将第一块空间赋予它 //1、3相等,1、2不等 } }
toString()方法
返回值类型String
可以根据程序需求重写该方法
package com.object.demo06; public class Test { public static void main(String[] args) { Student s1 = new Student("aaa", 20); Student s2 = new Student("bbb", 22); //3.toString System.out.println(s1.toString()); System.out.println(s2.toString()); } }
得到的是@+16进制哈希值
想要看到具体的属性就要对toString进行重写
package com.object.demo06; 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; } //快捷键alt+ins @Override public String toString() { return "Student [name=" + name + ", age=" + age + "]"; } }
equals()方法
返回布尔类型
package com.object.demo06; public class Test { public static void main(String[] args) { Student s1 = new Student("aaa", 20); Student s2 = new Student("bbb", 22); //4.equals方法:判断两个对象是否相等 System.out.println("-----------------------------------"); System.out.println(s1.equals(s2)); Student s4 = new Student("小明",17); Student s5 = new Student("小明", 17); System.out.println(s4.equals(s5)); //虽然两者的属性相同,但是分别开辟了一个内存空间 false } }
要让属性相同返回true就要对equals进行重写
@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()) { // return true; // } if (obj instanceof Student) { //instanceof判断对象是否是某种类型 //4.强制类型转换 Student s = (Student) obj; //5.比较属性 if (this.name.equals(s.getName()) && this.age == s.getAge()) { return true; } } return false; }
finalize()方法
该方法一般情况下,程序员不会调用
-
当对象被判定为垃圾对象时,由JVM自动调用此方法,用以标记垃圾对象,进入回收队列
-
垃圾对象:没有有效引用指向此对象时,为垃圾对象
-
垃圾回收:由GC销毁垃圾对象,释放数据存储空间
-
自动回收机制:JVM的内存耗尽,释放数据存储空间
-
手动回收机制:使用System.gc(),通知JVM执行垃圾回收
@Override protected void finalize() throws Throwable { System.out.println(this.name +"对象被回收了"); } //对finalize方法进行重写
package com.object.demo06; public class Test2 { public static void main(String[] args) { Student s1 = new Student("aaa", 20); new Student("bbb", 20); //回收垃圾 System.gc(); System.out.println("回收垃圾"); //aaa正常,bbb被回收 } }
包装类
-
基本数据所对应的引用数据类型
基本数据类型 | 包装类 |
---|---|
boolean | Boolean |
byte | Byte |
char | Character |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
类型转换与装箱拆箱
栈里面的对象拿到堆里面:装箱
堆里面的对象拿到栈里面:拆箱
package com.object.demo07; public class Test { public static void main(String[] args) { //类型转换:装箱:基本类型转成引用类型的过程 int num1 = 18; //基本类型 栈 //使用Integer类创建对象 下面两种方式都行 Integer integer1 = new Integer(num1); Integer integer2 = Integer.valueOf(num1); System.out.println("装箱"); System.out.println(integer1); System.out.println(integer2); //类型转换:拆箱:引用类型转成基本类型 Integer integer3 = new Integer(100); int num2 = integer3.intValue(); System.out.println("拆箱"); System.out.println(num2); //JDK1.5以后,提供自动装箱和拆箱 int age = 18; //自动装箱 Integer integer4 =age; System.out.println("自动装箱"); System.out.println(integer4); //自动拆箱 int age2 = integer4; System.out.println("自动拆箱"); System.out.println(age2); } }
基本类型转换
package com.object.demo07; public class Test { public static void main(String[] args) { //基本类型转成字符串 int n1 = 15; //1.使用+号 String s1 = n1 + ""; //2.使用Integer中的toString()方法 String s2 = Integer.toString(n1); String s3 = Integer.toString(n1,16); //toString重载方法,将它转成指定进制 System.out.println(s1); System.out.println(s2); System.out.println(s3); //字符串转成基本类型 String str = "150"; //使用Integer.parseXXX(); int n2 = Integer.parseInt(str); System.out.println(n2); //boolean字符串形式转成基本类型,"true"-->true,只要这个字符串不是true就是false String str2 = "true"; boolean b1 = Boolean.parseBoolean(str2); System.out.println(b1); } }
整数缓冲区
Java预先创建了256个常用的整数包装类型对象Integer
我们接下来从一个程序来加深这句话的理解
package com.object.demo07; public class Test2 { public static void main(String[] args) { //面试题 Integer integer1 = new Integer(100); Integer integer2 = new Integer(100); System.out.println(integer1 == integer2); //引用类型在堆内存开辟了两个不同空间 false Integer integer3 = 100; //自动装箱 即Integer integer3 = Integer.valueOf(100) 后续同理 Integer integer4 = Integer.valueOf(100); //自动装箱 System.out.println(integer3 == integer4); //integer3,integer4都是引用类型,答案也是false吗? 结果是true Integer integer5 = 200; Integer integer6 = 200; System.out.println(integer5 == integer6); //这三行和上面三行几乎没差别 结果却相反 是false } }
自动装箱是使用Integer.valueOf()
问题就出在这个方法,ctrl+鼠标看valueOf()源码
public static Integer valueOf(int i) { if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i); }
low= -128,high = 127;
Integer integer3 = 100; Integer integer4 = 100; System.out.println(integer3 == integer4); //true
100在-128~127 返回cache[] 数组
数组在缓冲区中有256个位置,将100这个值取地址给integer3和integer4,两者地址相同,所以返回true
Integer integer5 = 200; Integer integer6 = 200; System.out.println(integer5 == integer6); //false
200不在-128~127 return new Integer(i); 和Integer1,2一样,在堆内存开辟新空间