Java Interview Essentials: Methods of java.lang.Object
This article reviews the 13 methods of Java's java.lang.Object class, explaining their purpose, typical usage in interviews, and related follow‑up questions such as equals vs ==, hashCode relationship, thread communication, and garbage collection.
Click the "Java Interview Questions精选" above to follow the public account.
This article discusses a fundamental topic that many people overlook or answer incompletely, yet it is frequently asked in interviews.
Interview Question
How many methods does Object have?
Java is a single‑inheritance language; all classes share a common ancestor, the Object class. If a class does not explicitly extend another class, it implicitly extends Object. Object methods are used daily, but they can be confusing if you are not prepared for a sudden question.
Analysis
The Object class is the base class for all Java classes, located in the java.lang package, and contains 13 methods, as shown in the image below.
Detailed Answers
1. Object()
This is the constructor of the Object class. (Not a focus point)
2. registerNatives()
Used by the JVM to discover native functions; for example, java.lang.Object.registerNatives maps to the C function Java_java_lang_Object_registerNatives. (Not a focus point)
3. clone()
Creates a copy of the current object. It can be called only if the class implements the Cloneable interface; otherwise, CloneNotSupportedException is thrown. (May lead to design‑pattern questions)
4. getClass()
A final method that returns the runtime class of the object, equivalent to Object.class. (May lead to class‑loading or reflection questions)
5. equals()
Compares the contents of two objects for equality. By default, equals behaves like == unless overridden. (May lead to discussions about equals vs == and HashMap implementation)
6. hashCode()
Returns the object's hash code, often overridden together with equals to ensure equal objects have equal hash codes. (May lead to HashMap implementation discussion)
7. toString()
Returns a string representation of the object. (No special notes)
8. wait()
Causes the current thread to wait until another thread calls notify() or notifyAll() on the same object. (May lead to thread‑communication questions and differences between wait and sleep)
9. wait(long timeout)
Causes the current thread to wait until notified or the specified timeout elapses. (Same follow‑up as above)
10. wait(long timeout, int nanos)
Causes the current thread to wait until notified, interrupted, or the specified time elapses. (Same follow‑up as above)
11. notify()
Wakes up a single thread waiting on the object's monitor. (May lead to thread‑communication questions)
12. notifyAll()
Wakes up all threads waiting on the object's monitor. (May lead to thread‑communication questions)
13. finalize()
Invoked by the garbage collector when it determines that there are no more references to the object. (Not a focus point, but may lead to garbage‑collection questions)
Common Follow‑Up Questions
What is the difference between equals() and ==?
What is the relationship between hashCode() and equals()?
What is the difference between wait() and sleep()?
Why must hashCode be overridden when equals is overridden?
How does HashMap work internally?
Explain the class‑loading mechanism.
We will answer these common questions in subsequent articles; many developers already know the answers by heart.
Recent Issues
[Issue 05] How to guarantee message order in a message queue?
[Issue 06] Different implementations of the Singleton pattern
[Issue 07] How Redis implements distributed locks
Curated common interview questions and technical knowledge points to help developers fill gaps.
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.