Java常用类-Objec和Objects
Java常用类之Object类
基本介绍
包地址:java.lang.Object
Class Object
是类Object
结构的根。 每个class都有Object
作为超类。 所有对象(包括数组)都继承这个类。
构造方法
public Object();
方法摘要
使用范围 | 修饰符 | 返回类型 | 方法 | 介绍 |
---|---|---|---|---|
对象操作 | protected | Object | clone() | 创建并返回此对象的副本 |
boolean | equals(Object obj) | 比较对象是否与传入对象相同 | ||
protected | void | finalize() | 当垃圾收集确定不再有对该对象的引用时,垃圾收集器在对象上调用该对象。 | |
Class <?> | getClass() | 返回此对象的运行时类 | ||
int | hashCode() | 返回对象的哈希码值 | ||
String | toString() | 返回对象的字符串表示形式 | ||
线程操作 | void | notify() | 唤醒正在等待对象监视器的单个线程 | |
void | notifyAll() | 唤醒正在等待对象监视器的所有线程 | ||
void | wait() | 导致当前线程等待,直到另一个线程调用该对象的 notify()方法或 notifyAll()方法。 | ||
void | wait(long timeout) | 导致当前线程等待,直到另一个线程调用 notify()方法或该对象的 notifyAll()方法,或者指定的时间已过。 | ||
void | wait(long timeout , int nanos) | 导致当前线程等待,直到另一个线程调用该对象的 notify()方法或 notifyAll()方法,或者某些其他线程中断当前线程,或一定量的实时时间。 |
Objects类
包地址:java.util.Objects
Objects类是对象的工具类,里面的方法都是用来操作对象的。
方法摘要
使用范围 | 修饰符 | 返回类型 | 方法 | 介绍 |
---|---|---|---|---|
对象操作 | static | int | compare(T a, T b) | 比较对象a和对象b,如果ab相等,返回0,如果不相等调用c的compare方法 |
boolean | equals(Object a , Object b) | 比较对象a和对象b,使用的是第一参数的equls方法,两参数有一个为null返回false,如果都是null,则返回true | ||
boolean | deepEquals(Object a, Object b) | 比较两个对象是否深度相等,使用Arrays.deepEquals0(a, b) | ||
int | hash(Object... values) | 得到一列对象的hash code,实际调用的是Arrays.hashCode(values) | ||
int | hashCode(Object o) | 得到一个对象的hash code,如果参数为null,返回0 | ||
boolean | isNull(Object obj) | 如果对象是null,返回true,否则返回false | ||
boolean | nonNull(Object obj) | 如果对象是null,返回false,否则返回true | ||
< T > T | requireNonNull(T obj) | 判断对象是不是null,如果不是null返回对象本身,如果是null则抛出空指针异常 | ||
< T > T | requireNonNull(T obj, String message) | 判断对象是不是null,如果不是null返回对象本身,如果是null则抛出空指针异常,并把第二个参数写在异常信息中 | ||
< T > T | requireNonNull(T obj, Supplier |
判断对象是不是null,如果不是null返回对象本身,如果是null则抛出自定义异常 | ||
Sting | toString(Object o) | 调用对象的toString方法,如果参数是null,返回字符串null | ||
Sting | toString(Object o, String nullDefault) | 调用对象的toString方法,如果参数是null,返回第二个参数 |
Seven times have I despised my soul:
The first time when I saw her being meek that she might attain height.
The second time when I saw her limping before the crippled.
The third time when she was given to choose between the hard and the easy, and she chose the easy.
The fourth time when she committed a wrong, and comforted herself that others also commit wrong.
The fifth time when she forbore for weakness, and attributed her patience to strength.
The sixth time when she despised the ugliness of a face, and knew not that it was one of her own masks.
And the seventh time when she sang a song of praise, and deemed it a virtue.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧