Time is running out, get in the car.
First look at the Jdk8 of all the objects in Java in the Object.java class.
Residential address in Java.lang.
There are not a few methods in total, most of which are native methods.
The family property also dig out,
Object.c method static Jninativemethod methods[] = {{"Hashcode", "() I", (void *) &jvm_ihashcode},{"Wait", "(J) V", (void *) &jvm_monitorwait},{"Notify", "() v", (void *) &jvm_monitornotify},{"Notifyall", "() v", (void *) &jvm_ monitornotifyall},{"Clone", "() Ljava/lang/object;", (void *) &JVM_Clone},};
It seems very simple. Come here, please.
From Jane into the deep, please listen to the question
1 What is the difference between equals () and = =?
There is no difference between comparing 2 references to the same. obj = new Object (); The OBJ reference, for the moment, is understood as the address of the object.
public boolean equals (Object obj) {return this==obj}
However, many times, for the user, do not care to point to the same object, but two of the content is the same as long as the line, such as a string class. To compare the content after the subclass inherits, override this method.
2 The relationship between Equals () and hashcode ()?
In fact, from the JDK source code can not see what the relationship, there is no way, this is the interviewer's favorite question one of the questions.
Hashcode (), here is the discussion of the Father class object. The JVM tries to get hashcode directly from the object header to return the int type value, which is obtained by a series of complex operations, not the same as the reference.
If the hashcode is different, equals must not be the same, because an object cannot have 2 different hashcode values;
Equals, which indicates that pointing to the same object, hashcode must be the same;
Therefore, if the subclass override Equals (), it is generally recommended to rewrite hashcode (); The reason is that when the object is placed in the container, in order to improve efficiency, take advantage of the above features, first call hashcode, then equals.
3 Why use clone? How does clone work?
As a general rule, the new object is constructed by constructing the method. The object member variable value at this time is the default value, and if you want to copy a long one, you need to call Get/set to copy it individually.
If the Clone method, the JVM copies all member variable references directly. Note: This is a shallow copy. Base type, copy value, reference type replication reference.
Subclass to implement clone, the precondition implement cloneable, although just an empty interface, the JVM call will be done before validation.
If it is just a shallow copy, clone () {Super.clone ()}; If the requirement is deep, then it must be rewritten, one by one, containing the object in clone. Prototype in Java design mode.
4 Finalize ()?
Like and finally, the final keyword is put together. Keywords and methods do not matter at all.
This method is primarily called by the JVM and is called once before the reference is recycled. Would like to play a bit, considering the JDK9 has been deprecated, that even.
5 getclass ()?
The first time you encounter the legendary final method, the method cannot be override, Class<?> returns a class object with a generic (parameter).
Core: All objects in Java. In Java, each class has a corresponding class object. When the compilation is complete, in the generated. class file, a class object is generated that represents the type information for this class.
int should be a basic type, not an (object) reference type, not a new construct, and there is nothing wrong with it.
The base type has a class type, and there is only one way to get the class type, for example
Class intclass = Int.class;
Reference type
1 similar basic types, static fetch mode
Class intclass = String.class;
2 Object.java is not a static class method that is called by an object.
Test test =new Test (); Test.getclass ();
3 Dynamic acquisition, runtime exception
Class.forName ();
The three differences 1. Class Name: The JVM will use the class loader to load the class into memory (provided that the class has not been loaded into memory) and does not do the initialization of the class. Returns the object 2.class.forname ("class name string") of Class (Note: Class name string is package name + class name) Description: Loads the class and does the static initialization of the class, returning the object 3. Instance object. GetClass () Description: Static initialization of the class, non-static initialization; Returns the object that the reference O runtime really refers to (because: a reference to a child object may be assigned to a reference variable in the parent object) object of class that belongs to
6 thread communication, multi-threading, lock-related wait notify?
Prerequisites: Must be placed in the synchronized synchronization block for use. Generator-Consumer mode. Reentrantlock explicitly implemented the lock, monitor,condition. These JVMs implicitly implement
Wait notify why not put in the thread class?
Regardless of which thread is currently invoked to execute to obj.wait (), the current thread can enter the waiting queue, wait for other threads to execute obj.notify (remove from the node, put in the wait Wake queue), Monitorexit (Synchronous block and synchronous method the underlying public) calls Unpark wakeup. If defined in the thread class, each thread needs to know the state between each other, notify each other, requires direct communication, obj acts as shared memory, implements thread communication, and reduces coupling.
7toString Packaging uses the class Hashcode method slightly
Java Basics Interview 1 Object