当前位置:网站首页>What are the common methods of object
What are the common methods of object
2022-06-10 23:09:00 【Li_ XiaoJin】
Object Is the parent of all classes , Any class inherits by default Object.Object What methods do classes implement ?
(1)clone Method
Protection method , Implement shallow copy of objects , Only when Cloneable Interface to call this method , Otherwise throw CloneNotSupportedException abnormal .
(2)getClass Method
final Method , Get runtime type .
(3)toString Method
This method is used more , In general, subclasses have coverage .
(4)finalize Method
This method is used to release resources . Because it's not clear when the method will be called , Rarely used .
(5)equals Method
This method is a very important one . commonly equals and == It's different , But in Object The two are the same . Subclasses usually override this method .
(6)hashCode Method
This method is used for hash lookup , Rewrote equals Methods are usually rewritten hashCode Method . This method has hash function in some Collection It is used in .
In general, it must satisfy obj1.equals(obj2)==true. Can be launched obj1.hash- Code()==obj2.hashCode(), however hashCode Equality does not necessarily satisfy equals. But to improve efficiency , We should try to make the above two conditions close to equivalence .
(7)wait Method
wait The method is to make the current thread wait for the lock of the object , The current thread must be the owner of the object , That is, the lock with the object .wait() Method has been waiting for , Until the lock is obtained or interrupted .wait(long timeout) Set a timeout interval , If the lock is not obtained within the specified time, return .
After calling this method, the current thread goes to sleep , Until .
(1) Other threads called the object's notify Method .
(2) Other threads called the object's notifyAll Method .
(3) Other threads called interrupt Interrupt the thread .
(4) Time's up .
At this point, the thread can be scheduled , If it is interrupted, throw a InterruptedException abnormal .
(8)notify Method
This method wakes up a thread waiting on the object .
(9)notifyAll Method
This method wakes up all threads waiting on the object .
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package java.lang;
public class ObjectTest {
/**
* Construction method
*/
public Object() {
}
/**
* In order to make JVM Discover native features , They are named in a certain way .
* for example , about java.lang.Object.registerNatives,
* Corresponding C The function is named Java_java_lang_Object_registerNatives.
*
* By using registerNatives( Or rather ,JNI function RegisterNatives), You can name anything you want C function .
*/
private static native void registerNatives();
/**
* final Method , To get the type of runtime .
* This is what this method returns Object Class objects of objects / Runtime class object Class. Effect and Object.class identical .
*
* Object obj = new Object();
* obj.getClass() == Object.class
*
*/
public final native Class<?> getClass();
/**
* This method is used to return the physical address of the object where it is located ( Hash code value ),
* Often meet with equals Method to override at the same time , Make sure that two equal objects have equal hashCode.
*/
public native int hashCode();
/**
* equals Used to compare whether the contents of two objects are equal .
* By default ( Inherited from Object class ),equals and == It's the same , Unless it's overwritten (override) 了 .
*/
public boolean equals(Object var1) {
return this == var1;
}
/**
* clone() Function is used to save an existing object .
* Only when Cloneable Interface to call this method , Otherwise throw CloneNotSupportedException abnormal .
*/
protected native Object clone() throws CloneNotSupportedException;
/**
* toString() Method returns the string representation of the object , There is nothing to say about this method .
* Usually overwritten
*/
public String toString() {
return this.getClass().getName() + "@" + Integer.toHexString(this.hashCode());
}
/**
* Wake up a single thread waiting on this object monitor .(
*/
public final native void notify();
/**
* Wakes up all threads waiting on this object monitor .
*/
public final native void notifyAll();
/**
* Causes the current thread to wait , Until another thread calls the notify() Method or notifyAll() Method , Or more than the specified amount of time .
*/
public final native void wait(long var1) throws InterruptedException;
/**
* Causes the current thread to wait , Until another thread calls the notify() Method or notifyAll() Method ,
* Or some other thread breaks the current thread , Or has exceeded a certain amount of actual time .
*/
public final void wait(long var1, int var3) throws InterruptedException {
if (var1 < 0L) {
throw new IllegalArgumentException("timeout value is negative");
} else if (var3 >= 0 && var3 <= 999999) {
if (var3 > 0) {
++var1;
}
this.wait(var1);
} else {
throw new IllegalArgumentException("nanosecond timeout value out of range");
}
}
/**
* Causes the current thread to wait , Until another thread calls the notify() Method or notifyAll() Method .
*/
public final void wait() throws InterruptedException {
this.wait(0L);
}
/**
* When the garbage collector determines that there are no more references to the object , This method is called by the object's garbage collector .
*/
protected void finalize() throws Throwable {
}
static {
registerNatives();
}
}
Copyright: use Creative Commons signature 4.0 International license agreement to license Links:https://lixj.fun/archives/about-object-method
边栏推荐
- A journey of database full SQL analysis and audit system performance optimization
- 【006】初识字符串
- 项目实训13——界面补充
- Untiy reset animation
- 汇编:汇编与C派系语言混用以及对应LLDB常用指令
- [original] analysis of nine price HPV data capture of Yilu app
- PwnTheBox,Pwn:tutorial1
- Can Huawei matepad become the secondary screen of your laptop?
- Missing heritability
- Keras深度学习实战(8)——使用数据增强提高神经网络性能
猜你喜欢

UE4 getting started with bone animation

AI智能视频分析EasyCVR平台设备通道批量删除功能的开发实现

Distributed Foundation

Blue Bridge Cup_ A fool sends a letter_ recursion

Can Huawei matepad become the secondary screen of your laptop?

2022 t elevator repair test question simulation test question bank and online simulation test

数据与信息资源共享平台(四)

数据与信息资源共享平台(九)

数据与信息资源共享平台(五)

Reptiles learn knowledge
随机推荐
关于高考的那些事儿
盲盒商城APP系统开发的软件特点和盲盒功能介绍
数组、List、Set、Map、Properties依赖注入格式
Is it safe to open a BOC securities account? Is the risk high?
Assembly: assembly mixed with c-faction language and corresponding lldb common instructions
kubernetes多網卡方案之Multus CNI部署和基本使用
通达信股票开户安全吗?如何办理开户呢?
爬虫学习知识
软件测试入门之软件测试的概念与过程(精辟内容)
06151020 mysterious code, waiting for you to decipher
vulnhub之dc3
数据与信息资源共享平台(六)
Icml2022 | revoir la traduction vocale de bout en bout du texte à partir de zéro
laravel8 实现阿里云文件上传
SMB anonyme
数据与信息资源共享平台(八)
Dependencymanagement and dependencies
Image mosaic camera mosaic notes
Watlow signs agreement to acquire EUROTHERM from Schneider Electric
Software project management 6.10 Cost budget