How was Object class generated?
I am just curious to know how the classes (inside rt.jar provided by Oracle) like java.lang.Object, java.lang.String were generated from the .java source files. I think, it's not possible for their javac to compile them.
I tried to compile Dummy.java containing "class Dummy{}"
lab@labb:~/Documents$ set PATH=.:$JAVA_HOME/bin
lab@labb:~/Documents$ set CLASSPATH=.
lab@labb:~/Documents$ javac -verbose Dummy.java
[parsing started RegularFileObject[Dummy.java]]
[parsing completed 12ms]
[search path for source files: .]
[search path for class files: /usr/lib/jvm/java-7-oracle/jre/lib/resources.jar,/usr/lib/jvm/java-7-oracle/jre/lib/rt.jar,/usr/lib/jvm/java-7-oracle/jre/lib/sunrsasign.jar,/usr/lib/jvm/java-7-oracle/jre/lib/jsse.jar,/usr/lib/jvm/java-7-oracle/jre/lib/jce.jar,/usr/lib/jvm/java-7-oracle/jre/lib/charsets.jar,/usr/lib/jvm/java-7-oracle/jre/classes,/usr/lib/jvm/java-7-oracle/jre/lib/ext/sunpkcs11.jar,/usr/lib/jvm/java-7-oracle/jre/lib/ext/dnsns.jar,/usr/lib/jvm/java-7-oracle/jre/lib/ext/zipfs.jar,/usr/lib/jvm/java-7-oracle/jre/lib/ext/localedata.jar,/usr/lib/jvm/java-7-oracle/jre/lib/ext/sunec.jar,/usr/lib/jvm/java-7-oracle/jre/lib/ext/sunjce_provider.jar,.]
[loading ZipFileIndexFileObject[/usr/lib/jvm/java-7-oracle/lib/ct.sym(META-INF/sym/rt.jar/java/lang/ Object .class)]]
[checking Dummy]
[loading ZipFileIndexFileObject[/usr/lib/jvm/java-7-oracle/lib/ct.sym(META-INF/sym/rt.jar/java/lang/ AutoCloseable .class)]]
[wrote RegularFileObject[Dummy.class]] [total 131ms]
All classes can be expressed as Java code (as you can see here: http://www.docjar.com/docs/api/java/lang/package-index.html) and compiled by javac. Object
is a special case insofar as when loaded by the VM, it will not get a superclass.
You can use a decompiler (JAD by example) to see how would look source file for a .class.
In case of base classes there are some methods that need to be native
and their implementation provided in system-dependent libraries like dll
for Windows or so
for Linux.
String has only one method native (intern). Object has more. There are classes in JRE that needs native to acomplish things related to SO and are very low-level, like atomic values or threads (thanks @yshavit), but the vast majority of the JRE library code is programmed in Java.
链接地址: http://www.djcxy.com/p/73814.html下一篇: Object类是如何生成的?