Java.lang.Object Underlying code Analysis-jdk1.8

Source: Internet
Author: User
Tags throwable java se

First, let's say two things.

1, some of the following blog will be on the bottom of the JDK method for some analysis, because I personally developed more than 2 years, found that there are many things are not clear, when I have a good bottom source today is still a lot of harvest, so share with you, I just reference the relevant documents

Jdk1.8api:https://docs.oracle.com/javase/8/docs/api/index.html

Code: Https://gitee.com/luanmihun/java-jdk-study

2, why to analyze the version of jdk1.8, just a few personal analysis, but also hope to understand, if not the trouble you re-comment to me to propose changes in the comments I will promptly revise?

The main reason is that in my opinion, many companies are now using springboot to develop, because his automatic assembly is still very useful, springboot1.x version support jdk1.7 and 1.8, and the springboot2.x version has been supported to jdk1.9 (Currently the latest version is version 10, Java SE 9 has reached the end of support.)   Users of Java SE 9 should switch to Java SE 10 which is an official note), springboot2.x version is also supported jdk1.8 so here We choose the bottom of the jdk1.8 to explain, in fact, regardless of understanding the bottom of the version, and finally you just look at each update of the new features or can quickly follow the footsteps.

Now start the analysis of the Code

I have written the relevant comments in the code, so we can see the correlation analysis directly to the comment, the source can be directly on the git download

Our analysis of the underlying source code is all in the Rt.jar inside.

The following is all the methods inside the Java.lang.Object class

 PackageJava.lang;/*** @Auther: Luanmihun * @Date: 2018/7/30 17:02 * @Description: Java.lang package below the method analysis inside the object, on the inside of the method test class in Main.test.lang.ObjectMethodTest.java * object for all classes of the parent class, is defined in the jdk1.0 * There is currently no explanation for the problem with the deep understanding of the Clone () method and the Tohexstring () method Description, a description of the threading aspect*/ Public classObjectmethodexplain {/*** Native is the key word. It is generally declared locally, and is implemented offsite with C and C + +.     Its statement has a few points to note: * 1) The relationship between the native and the access control is not restricted.     * 2) must precede the return type.     * 3) It is generally a non-abstract class method. * 4) The native method is implemented in an offsite manner, like an abstract method, so there is no method body, ending with a semicolon * Description:. The contents of the remaining. dll files will be studied in depth, followed by updates*/    //The registernatives () method is loaded when the object is loaded and the. dll file is called    Private Static native voidregisternatives (); Static{registernatives (); }    /*** Returns the runtime class because it cannot be rewritten because it is final, and is tested in the main function, where two errors are returned * 1) java.lang.SecurityException:Prohibited package name: Java.lang project cannot be in Java. What makes a call to the package name * 2) java.lang.UnsatisfiedLinkError:ObjectMethod.registerNatives () V is because the registernatives () method should be loaded at the time of invocation * and in the code above we have written a registernatives () method, so cannot run, need to comment out the Registernatives () method*/    //Public Final native class<?> getclass ();    /*** Returns the hash code value of the object, supporting the hash table, for example: JAVA.UTIL.HASHMAP * Hashcode returned is not necessarily an object's (virtual) memory address, depending on the runtime library and the specific implementation of the JVM.     * Features are as follows: * 1) consistency (consistent), the same integer must be returned consistently for the same object during one execution of a program. * 2) If two objects are compared by Equals (object) and the result is equal, then calling the Hashcode method on each of these two objects should produce the same integer result, * here equals and hashcode say the object class of * 3) if two objects are compared by java.lang.Object.equals (Java.lang.Ojbect), the results are not equal, and you do not have to guarantee that the two objects are called Hashcode and return two different integers*/     Public native inthashcode (); /*** Compare the hashcode values of two objects *@paramobj passed in Object *@returnBoolean type*/     Public Booleanequals (Object obj) {return( This==obj); }    /*** Create and return a copy of this object, this method calls the class to implement the Cloneable interface, which involves the depth of copy, will be described later, If the Cloneable class is not implemented, the clonenotsupportedexception exception is not thrown *@return     * @throwsclonenotsupportedexception*/    protected nativeObject Clone ()throwsclonenotsupportedexception; /*** Returns an object name and the package name [email protected]+hashcode value, followed by a description of the tohexstring () method *@return     */     PublicString toString () {returnGetClass (). GetName () + "@" +integer.tohexstring (Hashcode ()); }    /*** Wake up a single thread waiting on this object's monitor*/    //Public Final native void notify ();    /*** Wake up all threads waiting on this object's monitor*/    //Public final native void Notifyall ();    /*** Causes the current thread to wait for the state to know that it is called by the Notify (), method, and Notifyall () method *@paramTimeout maximum wait time*/    //Public final native void wait (long timeout) throws interruptedexception;    /*** The current thread is in a waiting state known to be called by the Notify (), method, and Notifyall () method *@paramNanos extra time, 0-999999 in nanosecond range *@paramTimeout Maximum wait time **///Public final void wait (long timeout, int nanos) throws Interruptedexception {//if (Timeout < 0) {//throw new IllegalArgumentException ("Timeout value is negative");//        }////if (Nanos < 0 | | Nanos > 999999) {//throw new IllegalArgumentException (//"nanosecond timeout value out of range");//        }////if (Nanos > 0) {//timeout++;//        }////Wait (timeout);//    }    /*** The current thread is in a waiting state known to be called by Notify (), Method and Notifyall () method, default time is 0*///Public final void Wait () throws Interruptedexception {//Wait (0);//    }    /*** methods to be used by the garbage collection system *@throwsThrowable*/    protected voidFinalize ()throwsthrowable {}}

Here is the test class

 PackageTest.main.lang;/*** @Auther: Luanmihun * @Date: 2018/7/30 17:02 * @Description: Java.lang the method of the object in the test, the specific method, please refer to Java.lang.ObjectMethod*/ Public classObjectmethodtestImplementscloneable{ Public Static voidMain (string[] args)throwsException {objectmethodtest obj=Newobjectmethodtest (); //getclass () method testSystem.out.println ("GetClass () method test Result:" +Obj.getclass ()); //hashcode () method testSystem.out.println ("Hashcode () method test Result:" +Obj.hashcode ()); //equals () method testObjectmethodtest obj1 =Newobjectmethodtest (); System.out.println ("The hashcode value of obj is:" +Obj.hashcode ()); System.out.println ("The Hashcode value for Obj1 is:" +Obj1.hashcode ()); System.out.println (Equals () method test Result: "+obj.equals (obj)); System.out.println (Equals () method test Result: "+obj1.equals (obj)); //Clone () method testSystem.out.println ("Clone () method test Result:" +Obj.clone ()); //toString () method testSystem.out.println ("toString () method test Result:" +Obj.clone ()); }}

Java.lang.Object Underlying code Analysis-jdk1.8

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.