常用工具类介绍(1)API文档介绍&java.lang.Object类耦合度
- API文档介绍
- java.lang.Object类
- 1. 描述
- 2. equals : public boolean equals(Object obj)
- 3. hashCode : public native int hashCode()
- 4. toString : public String toString()
- 5. getClass() : public final native Class<?> getClass()
- 6. notify : public final native void notify()
- 7. notifyAll : public final native void notifyAll()
- 8. wait(long timeoutMillis) : public final native void wait(long timeoutMillis) throws InterruptedException
- 9. wait() : public final void wait() throws InterruPtedException
- 10. wait(long timeoutMillis) : public final void wait(long timeoutMillis, int nanos) throws InterruptedException
- 结语
API文档介绍
API(application programming interface),应用程序接口,是一种编程人员必备的手册。
由于每一门编程语言都有着庞大的语法方法库什么的,没有谁有把握能够记住所有的内容并能熟练的运用出来,故而世上才会有众多的手册用于查找,这个API文档就是起到一个字典的作用。
我现在使用的JDK版本是jdk11,但是也会结合jdk8来对照,可能会出现一些疏忽的地方,欢迎评论,感激不尽。
java.lang.Object类
1. 描述
JDK对其描述为:
Class {@code Object} is the root of the class
hierarchy.
Every class has {@code Object} as asuperclass.
All objects,including arrays, implement the methods of this class.
Object类是所有类
层次结构的根 。
每个类都有一个超类是Object类。
全部的对象,包括数组,都实现了Object类中的方法。
总体来说,其实就是:Object类是所有类的根类,所有类都继承了这个类,包括这个类的方法。
2. equals : public boolean equals(Object obj)
jdk中的这个方法的实现代码很简单。
public boolean equals(Object obj) {
return (this == obj);
}
分析:在Object类中,equals方法相当于两个对象使用“ == ”进行比较,所以这种情况下,a==b等同于a.equals(b),起不到该起的作用。
所以,在通常情况下,某对象使用equals进行正常比较的前提是,重写equals方法。
例:
import java.util.Objects;
public class Person {
private String name ;
private int age ;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return age == person.age &&
Objects.equals(name, person.name);
}
//通常equals和hashCode方法一起被重写
@Override
public int hashCode() {
return Objects.hash(name, age);
}
}
有一点需要注意,就是Objects.equals(name, person. name)这个地方,Objects类是一个工具类,位于java.util包下,jdk1.7之后出现的,如果是jdk1.5或者jdk1.6的话,可能重写的equals方法并不相同。
3. hashCode : public native int hashCode()
首先介绍一下native关键字:
native关键字修饰的方法是一个原生态方法,方法对应的实现不是靠java而是使用其他语言(C或C++等)。
描述:
hashCode方法返回的值是是内存地址的整数,和toString方法输出结果的后半部分数值相同,进制不同。
重写equals方法时需要重写此方法。
4. toString : public String toString()
jdk中关于这个方法的实现代码:
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
由此可见,在toString方法中调用了hashCode方法,而Integer.toHexString方法的解释是:
Returns a string
representationof the integerargumentas anunsignedinteger in base 16.
返回一个
无符号的十六进制整型参数的字符串表示形式。
Integer.toHexString实现了十进制和十六进制的转换。
建议所有子类都重写toString方法。
5. getClass() : public final native Class<?> getClass()
final关键字代表此方法是最终方法,不能被重写。
Native关键字代表此方法是原生态方法。
此方法的返回值是Class类型对象。
代码实例:
package com.newre.test;
class Person {
}
public class Test01 {
public static void main(String[] args) {
Person person = new Person() ;
System.out.println(person.getClass());
System.out.println(person.getClass().getName());
System.out.println(person.getClass().getSimpleName());
System.out.println(person.getClass().getSuperclass());
}
}
执行结果:
class com.newre.test.Person
com.newre.test.Person
Person
class java.lang.Object
6. notify : public final native void notify()
唤醒正在等待的单个线程。
7. notifyAll : public final native void notifyAll()
唤醒正在等待的全部线程。
8. wait(long timeoutMillis) : public final native void wait(long timeoutMillis) throws InterruptedException
等待最长timeoutMillis毫秒后被唤醒(中途可被唤醒)
InterruptedException :中断异常
9. wait() : public final void wait() throws InterruPtedException
里面调用了第8个方法,即wait(0),导致当前线程等待,如果没有被唤醒,则一直等待。
10. wait(long timeoutMillis) : public final void wait(long timeoutMillis, int nanos) throws InterruptedException
参数:
timeout - 以毫秒为单位等待的最长时间。
nanos - 额外的时间,以纳秒范围0-999999。
结语
这个是我按照自己对于这些方法的熟悉程度排序的,最后关于线程的只用过几次,不太熟悉,所以直接参照API文档中相关内容而写。