Here’s a basic question that many people ignore or at least say is incomplete, but there’s a good chance you’ll be asked in an interview.

The interview questions

How many methods can Object have?

  • The Java language is a single-inheritance structured language in which all classes have a common ancestor. That ancestor is the Object class.

  • If a class does not explicitly extend from a class with extends, it defaults to the Object class.

  • The Object method is basically used in ordinary times, but if we are not prepared for such a sudden question, or a little confused.

Analysis of the

The Object class is the base class for all classes in Java. There are 13 methods in the java.lang package. The diagram below: Specific solutions

1.Object() This is nothing to say, the constructor of the Object class. (Not important)

2. RegisterNatives () They are named in a certain way to enable the JVM to discover native functionality. For example, for Java. Lang. Object. RegisterNatives, corresponding C function named Java_java_lang_Object_registerNatives.

By using registerNatives (or more specifically, the JNI function registerNatives), you can name any C function you want. (Not important)

The clone() function is used to save an existing object. Only implements the Cloneable interface can call this method, otherwise throw CloneNotSupportedException anomalies. (Note: This may lead to questions about design patterns.)

4. GetClass () final method, used to get the type of the runtime. This method returns the Class/runtime Class of this Object. The effect is the same as object.class. (Note: the answer to this question may lead to the class loading, reflection, etc.)

5. Equals () equals is used to compare whether the contents of two objects are equal. By default (inherited from the Object class), equals and == are the same unless overridden. (Note: this may lead to the more common question of “equals vs. ==” and how hashMap works.)

6. HashCode () This method is used to return the physical address (hash value) of the object on which it resides. It is often overridden with equals to ensure that equal objects have equal Hashcodes. (Again, the question that might lead to the implementation of hashMap)

The toString() method returns a string representation of the object, and there’s nothing special about this method.

8. Wait () causes the current thread to wait until another thread calls notify() or notifyAll() on this object. What is the difference between wait and sleep?

9. Wait (long timeout) causes the current thread to wait until another thread calls notify() or notifyAll() of the object, or the specified amount of time is exceeded. What is the difference between wait and sleep?

10. Wait (long timeout, int nanos) causes the current thread to wait until another thread calls notify() or notifyAll(), or another thread interrupts the current thread, or a certain amount of time has elapsed. What is the difference between wait and sleep?

11. Notify () wakes up a single thread waiting on this object’s monitor. (Thread communication)

12. NotifyAll () wakes up all threads waiting on the monitor of this object. (Thread communication)

13. Finalize () This method is called by the object’s garbage collector when the garbage collector determines that no more references to the object exist. (Not the point, but be careful to introduce the garbage collection question)

Further frequently asked Questions

  • What is the difference between equals() and ==?

  • What is the connection between hashCode() and equals()?

  • The difference between wait() and sleep() methods

  • Why does overriding equals mean overwriting hashCode

  • Implementation principle of HashMap

  • Talk about the class loading mechanism

We’ll take a look at some answers to these common questions, which most of you have probably learned by heart from the same questions you’ve been asked 800 times.