Java-API常用类
目录
Java-API(Application Programming Interface)
public int hashCode()
public final Class getClass()
public String toString()
public boolean equals(Object obj)
protected void finalize()
protected Object clone()
1 public int hashCode():返回该对象的哈希码值
注意:哈希值是根据哈希算法计算出来的一个值,这个值和地址值有关,但是不是实际地址值。你可以理解为地址值。
Student s1 = new Student();
System.out.println(s1.hashCode()); // 11299397
2 public final Class getClass():返回此 Object 的运行时类
注意:public String getName():以 String 的形式返回此 Class 对象所表示的实体
public class StudentTest {
public static void main(String[] args) {
Student s = new Student();
Class c = s.getClass();
// out>>: class cn.itcast_01.Student
String str = c.getName();
System.out.println("str=c.getName()="+c.getName());
//out>>: cn.itcast_01.Student
//链式编程
String str2 = s.getClass().getName();
System.out.println("s.getClass().getName="+s.getClass().getName());
//out>>: s.getClass().getName=cn.itcast_01.Student
}
}
输出:
c = s.getClass()=class cn.itcast_01.Student
str=c.getName()=cn.itcast_01.Student
s.getClass().getName=cn.itcast_01.Student
3 public String toString():返回该对象的字符串表示
注意:Integer类下的一个静态方法:
public static String toHexString(int i):把一个整数转成一个十六进制表示的字符串
//重写to String() {
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
//返回的值这样才有意义
}
public class StudentDemo {
public static void main(String[] args) {
Student s = new Student();
System.out.println("s.hashCode()="+s.hashCode());
System.out.println("s.getClass().getName()="+s.getClass().getName());
System.out.println("--------------------");
System.out.println("s.toString()="+s.toString());// cn.itcast_02.Student@42552c
System.out.println("----sys----------------");
System.out.println(s.getClass().getName() + '@'
+ Integer.toHexString(s.hashCode()));
// 直接输出public String toString()重写的返回值
System.out.println(s);
}
}
输出:
s.hashCode()=366712642
s.getClass().getName()=cn.itcast_02.Student
--------------------
s.toString()=Student [name=null, age=0]
----sys----------------
cn.itcast_02.Student@15db9742
Student [name=null, age=0]
4 public boolean equals(Object obj)
注意:public boolean equals(Object obj):指示其他某个对象是否与此对象“相等”。
这个方法,默认情况下比较的是地址值。比较地址值一般来说意义不大,所以我们要重写该方法。
怎么重写呢?
一般都是用来比较对象的成员变量值是否相同。
重写的代码优化:提高效率,提高程序的健壮性。
最终版:
其实还是自动生成。
* 看源码:
* public boolean equals(Object obj) {
* //this - s1
* //obj - s2
* return (this == obj);
* }
public class StudentDemo {
public static void main(String[] args) {
Student s1 = new Student("林青霞", 27);
Student s2 = new Student("林青霞", 27);
System.out.println(s1 == s2); // false
Student s3 = s1;
System.out.println(s1 == s3);// true
System.out.println("---------------");
System.out.println(s1.equals(s2)); // obj = s2; //false
System.out.println(s1.equals(s1)); // true
System.out.println(s1.equals(s3)); // true
Student s4 = new Student("风清扬",30);
System.out.println(s1.equals(s4)); //false
Demo d = new Demo();
System.out.println(s1.equals(d)); //ClassCastException
}
}
输出:
false
true
---------------
false
true
true
false
false
//public class Student 类当中
//String的equals()方法是重写自Object类的,比较的是字符串的内容是否相同
//this -- s1
//obj -- s2
@Override
public boolean equals(Object obj) {
Student s = (Student)obj; //s -- obj -- s2; //向下转型
if(this.name.equals(s.name) && this.age == s.age) {
return true;
}else {
return false;
}
或者:
// Student s = (Student)obj;
// //System.out.println("同一个对象,还需要向下转型并比较吗?");
// return this.name.equals(s.name) && this.age == s.age;
//public class StudentDemo
public class StudentDemo {
public static void main(String[] args) {
Student s1 = new Student("林青霞", 27);
Student s2 = new Student("林青霞", 27);
Student s3 = s1;
System.out.println(s1.equals(s2)); // obj = s2; //false
System.out.println(s1.equals(s1)); // true
System.out.println(s1.equals(s3)); // true
Student s4 = new Student("风清扬",30);
System.out.println(s1.equals(s4)); //false
}
}
输出:
true
true
true
false
@Override
public boolean equals(Object obj) {
//为了提高效率
if(this == obj){
return true;
}
Student s = (Student)obj;
System.out.println("同一个对象,还需要向下转型并比较吗?");
return this.name.equals(s.name) && this.age == s.age;
}
}
public class StudentDemo {
public static void main(String[] args) {
Student s1 = new Student("林青霞", 27);
Student s2 = new Student("林青霞", 27);
Student s3 = s1;
System.out.println(s1.equals(s2)); // obj = s2; //false
System.out.println(s1.equals(s1)); // true
System.out.println(s1.equals(s3)); // true
Student s4 = new Student("风清扬",30);
System.out.println(s1.equals(s4)); //false
}
}
输出:
同一个对象,还需要向下转型并比较吗?
true
true
true
同一个对象,还需要向下转型并比较吗?
false
//优化的写法
@Override
public boolean equals(Object obj) {
if(this == obj){
return true;
}
if(!(obj instanceof Student)){
return false;
}
Student s = (Student)obj;
return this.name.equals(s.name) && this.age == s.age;
}
//自动的写法
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
5 protected void finalize():返回该对象的字符串表示
当垃圾回收器确定不存在对该对象的更多引用时,由对象的垃圾回收器调用此方法。用于垃圾回收,但是什么时候回收不确定。
6 protected Object clone():创建并返回此对象的一个副本
A:重写该方法
Cloneable:此类实现了 Cloneable 接口,以指示 Object.clone() 方法可以合法地对该类实例进行按字段复制。
这个接口是标记接口,告诉我们实现该接口的类就可以实现对象的复制了。
public class StudentDemo {
public static void main(String[] args) throws CloneNotSupportedException {
//创建学生对象
Student s = new Student();
s.setName("林青霞");
s.setAge(27);
//克隆学生对象
Object obj = s.clone();
Student s2 = (Student)obj;
System.out.println("---------");
System.out.println(s.getName()+"---"+s.getAge());
System.out.println(s2.getName()+"---"+s2.getAge());
//以前的做法
Student s3 = s;
System.out.println(s3.getName()+"---"+s3.getAge());
System.out.println("---------");
//其实是有区别的
s3.setName("刘意");
s3.setAge(30);
System.out.println(s.getName()+"---"+s.getAge());
System.out.println(s2.getName()+"---"+s2.getAge());
System.out.println(s3.getName()+"---"+s3.getAge());
}
}
/*
//<1>克隆学生对象
Object obj = s.clone();
Student s2 = (Student)obj;
//<2>以前的做法
Student s3 = s;
<1>和<2>的区别:<1>是复制(克隆)一份一模一样的;<2>是指向同一个对象
*/