日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java类的加载

發(fā)布時(shí)間:2024/4/14 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java类的加载 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1 jvm默認(rèn)有3類class loader

bootstrap class loader,啟動(dòng)類加載器,負(fù)責(zé)加載${java_home}/jre/lib目錄下的庫,比如java基礎(chǔ)類庫包rt.jar

extension class loader,擴(kuò)展類加載器,負(fù)責(zé)加載${java_home}/jre/lib/ext目錄下的庫,即第三房擴(kuò)展庫。

application class loader,應(yīng)用程序類加載器,負(fù)責(zé)加載classpath指定的用戶的類庫。

2 加載什么

class loader加載的是庫中的類,而不是整個(gè)庫加載進(jìn)jvm。加載進(jìn)整個(gè)庫是完全沒有必要的,是非常浪費(fèi)內(nèi)存的,這個(gè)和動(dòng)態(tài)鏈接庫不同,動(dòng)態(tài)鏈接庫是整個(gè)都要加載進(jìn)內(nèi)存中。

3 加載一個(gè)類的過程

bootstrap class loader是extention class loader的父類,extension class loader是application class loader的父類。

如果一個(gè)類加載器收到了一個(gè)類加載的請(qǐng)求,它不會(huì)自己去加載,而是讓父加載器先加載,只有當(dāng)父加載器加載不了時(shí),它才去加載。

這樣設(shè)計(jì)的好處是明顯的,保證了Object類在程序中是唯一的,無論是誰加載,最終都是bootstrap class loader從rt.jar中加載的Object類。否則java程序就回混亂了。

4 類加載的時(shí)機(jī)

當(dāng)要用到某個(gè)類的時(shí)候才會(huì)去加載這個(gè)類,否則是不會(huì)加載這個(gè)類的。具體來說,就是出現(xiàn)了new關(guān)鍵字,需要?jiǎng)?chuàng)建這個(gè)類的對(duì)象的時(shí)候,會(huì)去加載這個(gè)類。加載這個(gè)類的時(shí)候,class loader是去相應(yīng)的目錄下根據(jù)包名找到這個(gè)類,然后加載進(jìn)來。

所以,類加載三點(diǎn)是很重要的東西:第一,new關(guān)鍵字,第二,import時(shí)候的包名,第三,類加載器的搜索目錄,它會(huì)去這個(gè)目錄下搜索第二個(gè)信息指定的類。

驗(yàn)證手段如下:

The rules for loading classes are spelled out in detail in the JVM specification. The basic principle is that classes are only loaded when needed (or at least appear to be loaded this way -- the JVM has some flexibility in the actual loading, but must maintain a fixed sequence of class initialization). Each class that gets loaded may have other classes that it depends on, so the loading process is recursive. The classes in Listing 2 show how this recursive loading works. The Demo class includes a simple main method that creates an instance of Greeter and calls the greet method. The Greeter constructor creates an instance of Message, which it then uses in the greet method call.

source code:

public class Demo { ????public static void main(String[] args) { ????????System.out.println("**beginning execution**"); ????????Greeter greeter = new Greeter(); ????????System.out.println("**created Greeter**"); ????????greeter.greet(); ????} } public class Greeter { ????private static Message s_message = new Message("Hello, World!"); ????? ????public void greet() { ????????s_message.print(System.out); ????} } public class Message { ????private String m_text; ????? ????public Message(String text) { ????????m_text = text; ????} ????? ????public void print(java.io.PrintStream ps) { ????????ps.println(m_text); ????} } Setting the parameter -verbose:class on the java command line prints a trace of the class loading process. Listing 3 shows partial output from running the Listing 2 program with this parameter: [Opened /usr/java/j2sdk1.4.1/jre/lib/rt.jar] [Opened /usr/java/j2sdk1.4.1/jre/lib/sunrsasign.jar] [Opened /usr/java/j2sdk1.4.1/jre/lib/jsse.jar] [Opened /usr/java/j2sdk1.4.1/jre/lib/jce.jar] [Opened /usr/java/j2sdk1.4.1/jre/lib/charsets.jar] [Loaded java.lang.Object from /usr/java/j2sdk1.4.1/jre/lib/rt.jar] [Loaded java.io.Serializable from /usr/java/j2sdk1.4.1/jre/lib/rt.jar] [Loaded java.lang.Comparable from /usr/java/j2sdk1.4.1/jre/lib/rt.jar] [Loaded java.lang.CharSequence from /usr/java/j2sdk1.4.1/jre/lib/rt.jar] [Loaded java.lang.String from /usr/java/j2sdk1.4.1/jre/lib/rt.jar] ... [Loaded java.security.Principal from /usr/java/j2sdk1.4.1/jre/lib/rt.jar] [Loaded java.security.cert.Certificate ??from /usr/java/j2sdk1.4.1/jre/lib/rt.jar] [Loaded Demo] **beginning execution** [Loaded Greeter] [Loaded Message] **created Greeter** Hello, World! [Loaded java.util.HashMap$KeySet ??from /usr/java/j2sdk1.4.1/jre/lib/rt.jar] [Loaded java.util.HashMap$KeyIterator ??from /usr/java/j2sdk1.4.1/jre/lib/rt.jar]

This is only a partial listing of the most important parts -- the full trace consists of 294 lines, most of which I deleted for this listing. The initial set of class loads (279, in this case) are all triggered by the attempt to load the Demo class. These are the core classes that are used by every Java program, no matter how small. Even eliminating all the code from the Demo main method doesn't affect this initial sequence of loads. The number and names of classes involved will differ from one version of the class libraries to another, though.

The portion of the listing after the Demo class is loaded is more interesting. The sequence here shows that the Greeter class is only loaded when an instance of the class is about to be created. However, the Greeter class uses a static instance of the Message class, so before an instance of the former can be created, the latter class also needs to be loaded.

A lot happens inside the JVM when a class is loaded and initialized, including decoding the binary class format, checking compatibility with other classes, verifying the sequence of bytecode operations, and finally constructing a java.lang.Class instance to represent the new class. This Class object becomes the basis for all instances of the new class created by the JVM. It's also the identifier for the loaded class itself -- you can have multiple copies of the same binary class loaded in a JVM, each with its own Class instance. Even though these copies all share the same class name, they will be separate classes to the JVM.

?參考資料:

1. https://www.ibm.com/developerworks/library/j-dyn0429/

轉(zhuǎn)載于:https://www.cnblogs.com/hustdc/p/6431686.html

超強(qiáng)干貨來襲 云風(fēng)專訪:近40年碼齡,通宵達(dá)旦的技術(shù)人生

總結(jié)

以上是生活随笔為你收集整理的java类的加载的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。