This has been written by the author of the third Java notes, fortunately, the first two articles have been friends of the Forum to encourage the support, but also hope that we continue to correct the deficiencies. The author also has been eager to sober self-trial in such a way, to find their own technical shortcomings, hope and common love Java colleagues to improve together.
The first two separate interpretations of the JVM, JDK, JRE, collection, ClassLoader, and some design pattern. This is still not ready to start the transition to the Java EE, because I think there are some trivial j2se of the problem has not been summed up.
1. About Object class understanding
Everyone knows that object is the base class for all Java classes, meaning that all Java classes inherit the 11 methods of object. It is recommended that you look at the source code of the 11 member functions of object, and you will know the default implementation. For example, the Equals method, the default implementation is to use "= =" to compare, that is, directly compare the memory address, return TRUE or false. and ToString () method, the return of the string composition of the way is??
"GetClass (). GetName () + "@" + integer.tohexstring (hashcode ()) "In fact, I do not have too much explanation, we can understand the composition of this string." Next look at Hashcode (): Public native int hashcode ();
Because it's a native approach, it's related to the way the OS is handled, and there's only one statement in the source code. If we are interested, we can go into the hashcode of how the OS produced it? But the author suggested that the most important thing is to remember to use it a few principles! First, if the Equals () method has the same object with the same hashcode, but the Equals () object is not connected, it does not guarantee that the Hashcode () method returns a different integer. And the next time you run the same program, the same object may not be the original hashcode () Oh.
What about the rest of the way? Nofigy (), Notifyall (), Clone (), wait () are native methods that are dependent on the implementation of the operating system. The last interesting approach is finalize (), a destructor similar to C + +, where the signature is protected, proving that only inherited extensions can be used, the method body is empty, and nothing is implied. Its role according to the author's understanding is only to inform the JVM this object is no longer used, can be destroyed at any time, and the actual destruction right is still on the virtual operator. So it's really not doing anything? Not necessarily, in fact, if it is a thread object, it can lead to a higher priority for the thread in a certain range, resulting in faster destruction to conserve memory and improve performance. In fact, from the commonsense point of view, we can probably guess the purpose of the JVM approach.
2. On the relationship between overloaded hashcode () and the collection framework
I have listened to a Java training for many years predecessors said in his view Hashcode method does not make any sense, just to cooperate with the proof of the same hashcode will lead to equal and the Equals method exists. Even some predecessors make such a mistake, in fact, it is still full of easy to ignore. So what does the Hashcode () method do?
The course of data structure you will know that there is a structure called hash table to improve the efficiency of queries by assigning a unique index to each object. Then Java will not arbitrarily distort the concept of change, so the only function of hashcode is to support the structure of the hash table in the existence of, in other words, only the Hashtable of the set frame, HashMap, HashSet, It is necessary to overload the Hashcode () method so that we can artificially control whether the indexes are equal in the hash structure. I cite an example: once in order to write a solution class program, you need to list the 1,2,3,4 composition of the different permutations and combinations, so I wrote an array class with int[] to save the combination of results, and then the randomly generated combination into a hashset, is to use HashSet not to include the characteristics of repeating elements. But how does HashSet judge the element of repetition? Of course, it is judged by the equality of the results returned by the hashcode (), this experiment can be done:
int[] A = {1,2,3,4};
int[] B = {1,2,3,4};
System.out.println(A.hashCode());
System.out.println(B.hashCode());