銆銆路JRE(Java杩愯岀幆澧)锛氭墍鏈夌殑Java 绋嬪簭閮借佸湪JRE涓嬫墠鑳借繍琛屻
銆銆路JDK锛氬紑鍙戣呯紪璇戙佽皟璇昷ava绋嬪簭鐢ㄧ殑寮鍙戝伐鍏峰寘銆侸DK鐨勫伐鍏蜂篃鏄疛ava绋嬪簭锛屼篃闇瑕丣RE鎵嶈兘杩愯屻傚湪JDK鐨勫畨瑁呯洰褰曚笅鏈変竴涓鍚嶄负jre鐨勭洰褰曪紝鐢ㄤ簬瀛樻斁JRE鏂囦欢銆
銆銆路JVM(Java铏氭嫙鏈)鏄疛RE鐨勪竴閮ㄥ垎銆傚畠鏄涓涓铏氭瀯鍑烘潵鐨勮$畻鏈猴紝鏄閫氳繃鍦ㄥ疄闄呯殑璁$畻鏈轰笂浠跨湡妯℃嫙鍚勭嶈$畻鏈哄姛鑳芥潵瀹炵幇鐨勩侸VM鏈夎嚜宸卞畬鍠勭殑纭浠舵灦鏋勶紝濡傚勭悊鍣ㄣ佸爢鏍堛佸瘎瀛樺櫒绛夛紝杩樺叿鏈夌浉搴旂殑鎸囦护绯荤粺銆備娇鐢↗VM灏辨槸涓轰簡鏀鎸佷笌鎿嶄綔绯荤粺鏃犲叧锛屽疄鐜拌法骞冲彴銆侸VM鍦ㄦ墽琛屽瓧鑺傜爜鏃讹紝瀹為檯涓婃渶缁堣繕鏄鎶婂瓧鑺傜爜瑙i噴鎴愬叿浣撳钩鍙颁笂鐨勬満鍣ㄦ寚浠ゆ墽琛屻
銆銆JVM 绫诲姞杞芥満鍒惰﹁В锛
銆銆JVM绫诲姞杞芥満鍒跺垎涓轰簲涓閮ㄥ垎锛
銆銆路鍔犺浇锛
銆銆閫氳繃涓涓猔绫荤殑鍏ㄩ檺瀹氬悕`鏉ヨ幏鍙栧畾涔夋ょ被鐨勪簩杩涘埗瀛楄妭娴;
銆銆灏嗚繖涓瀛楄妭娴佹墍浠h〃鐨勯潤鎬佸瓨鍌ㄧ粨鏋勮浆鍖栦负鏂规硶鍖虹殑杩愯屾椂鏁版嵁缁撴瀯;
銆銆鍦ㄥ唴瀛樹腑鐢熸垚涓涓杩欎釜绫荤殑`java.lang.Class`瀵硅薄锛屼綔涓烘柟娉曞尯杩欎釜绫荤殑鍚勭嶆暟鎹鐨勮块棶鍏ュ彛;
銆銆路楠岃瘉 锛氫负浜嗙‘淇滳lass鏂囦欢鐨勫瓧鑺傛祦涓鍖呭惈鐨勪俊鎭绗﹀悎褰撳墠铏氭嫙鏈虹殑瑕佹眰锛屽苟涓斾笉浼氬嵄瀹宠櫄鎷熸満鑷韬鐨勫畨鍏ㄣ傚寘鎷鏂囦欢鏍煎紡楠岃瘉锛屽厓鏁版嵁楠岃瘉锛屽瓧鑺傜爜楠岃瘉锛岀﹀彿寮曠敤楠岃瘉銆傚彲浠ラ噰鐢-Xverifynone鍙傛暟鏉ュ叧闂澶ч儴鍒嗙殑绫婚獙璇佹帾鏂斤紝浠ョ缉鐭铏氭嫙鏈虹被鍔犺浇鐨勬椂闂
銆銆路鍑嗗囷細涓虹被鍙橀噺鍒嗛厤鍐呭瓨骞惰剧疆绫诲彉閲忓垵濮嬪肩殑闃舵碉紝杩欎簺鍙橀噺鎵浣跨敤鐨勫唴瀛橀兘灏嗗湪鏂规硶鍖轰腑杩涜屽垎閰嶃
public static int v = 8080; #鍙橀噺v鍦ㄥ噯澶囬樁娈佃繃鍚庣殑鍒濆嬪间负0鑰屼笉鏄8080锛屽皢v璧嬪间负8080鐨刾utstatic鎸囦护鏄绋嬪簭琚缂栬瘧鍚庯紝瀛樻斁浜庣被鏋勯犲櫒()鏂规硶涔嬩腑锛屾妸value璧嬪间负123鐨勫姩浣滃皢鍦ㄥ垵濮嬪寲闃舵垫墠浼氭墽琛屻 public static final int v = 8080; #鍦ㄧ紪璇戦樁娈典細涓簐鐢熸垚`ConstantValue`灞炴э紝鍦ㄥ噯澶囬樁娈佃櫄鎷熸満浼氭牴鎹甡ConstantValue`灞炴у皢v璧嬪间负8080銆銆銆路瑙f瀽锛氭槸铏氭嫙鏈哄皢甯搁噺姹犱腑鐨勭﹀彿寮曠敤鏇挎崲涓虹洿鎺ュ紩鐢ㄧ殑杩囩▼銆傜﹀彿寮曠敤灏辨槸class鏂囦欢涓鐨凜ONSTANT_Class_info锛孋ONSTANT_Field_info锛孋ONSTANT_Method_info
銆銆绗﹀彿寮曠敤锛氬紩鐢ㄧ殑鐩鏍囧苟涓嶄竴瀹氳佸凡缁忓姞杞藉埌鍐呭瓨涓銆傚悇绉嶈櫄鎷熸満瀹炵幇鐨勫唴瀛樺竷灞鍙浠ュ悇涓嶇浉鍚岋紝浣嗘槸瀹冧滑鑳芥帴鍙楃殑绗﹀彿寮曠敤蹇呴』鏄涓鑷寸殑锛屽洜涓虹﹀彿寮曠敤鐨勫瓧闈㈤噺褰㈠紡鏄庣‘瀹氫箟鍦↗ava铏氭嫙鏈鸿勮寖鐨凜lass鏂囦欢鏍煎紡涓銆傜洿鎺ュ紩鐢锛氭寚鍚戠洰鏍囩殑鎸囬拡锛岀浉瀵瑰亸绉婚噺鎴栨槸涓涓鑳介棿鎺ュ畾浣嶅埌鐩鏍囩殑鍙ユ焺銆傚傛灉鏈変簡鐩存帴寮曠敤锛岄偅寮曠敤鐨勭洰鏍囧繀瀹氬凡缁忓湪鍐呭瓨涓瀛樺湪銆
銆銆路鍒濆嬪寲锛氬垵濮嬪寲闃舵垫槸鎵ц岀被鏋勯犲櫒鏂规硶鐨勮繃绋嬨傛柟娉曟槸鐢辩紪璇戝櫒鑷鍔ㄦ敹闆嗙被涓鐨勭被鍙橀噺鐨勮祴鍊兼搷浣滃拰闈欐佽鍙ュ潡涓鐨勮鍙ュ悎骞惰屾垚鐨勩傝櫄鎷熸満浼氫繚璇佹柟娉曟墽琛屼箣鍓嶏紝鐖剁被鐨勬柟娉曞凡缁忔墽琛屽畬姣曘傚傛灉涓涓绫讳腑娌℃湁瀵归潤鎬佸彉閲忚祴鍊间篃娌℃湁闈欐佽鍙ュ潡锛岄偅涔堢紪璇戝櫒鍙浠ヤ笉涓鸿繖涓绫荤敓鎴()鏂规硶銆
銆銆浠涔堟椂鍊欓渶瑕佸圭被杩涜屽垵濮嬪寲?
銆銆浣跨敤new璇ョ被瀹炰緥鍖栧硅薄鐨勬椂鍊;
銆銆璇诲彇鎴栬剧疆绫婚潤鎬佸瓧娈电殑鏃跺(浣嗚玣inal淇楗扮殑瀛楁碉紝鍦ㄧ紪璇戝櫒鏃跺氨琚鏀惧叆甯搁噺姹犵殑闈欐佸瓧娈甸櫎澶杝tatic final);
銆銆璋冪敤绫婚潤鎬佹柟娉曠殑鏃跺;
銆銆浣跨敤鍙嶅皠Class.forName(鈥渪xxx鈥)瀵圭被杩涜屽弽灏勮皟鐢ㄧ殑鏃跺欙紝璇ョ被闇瑕佸垵濮嬪寲;
#p#分页标题#e#銆銆鍒濆嬪寲涓涓绫荤殑鏃跺欙紝鏈夌埗绫伙紝鍏堝垵濮嬪寲鐖剁被(娉锛1. 鎺ュ彛闄ゅ栵紝鐖舵帴鍙e湪璋冪敤鐨勬椂鍊欐墠浼氳鍒濆嬪寲;2.瀛愮被寮曠敤鐖剁被闈欐佸瓧娈碉紝鍙浼氬紩鍙戠埗绫诲垵濮嬪寲);
銆銆琚鏍囨槑涓哄惎鍔ㄧ被鐨勭被(鍗冲寘鍚玬ain()鏂规硶鐨勭被)瑕佸垵濮嬪寲;
銆銆褰撲娇鐢↗DK1.7鐨勫姩鎬佽瑷鏀鎸佹椂锛屽傛灉涓涓猨ava.invoke.MethodHandle瀹炰緥鏈鍚庣殑瑙f瀽缁撴灉REF_getStatic銆丷EF_putStatic銆丷EF_invokeStatic鐨勬柟娉曞彞鏌勶紝骞朵笖杩欎釜鏂规硶鍙ユ焺鎵瀵瑰簲鐨勭被娌℃湁杩涜岃繃鍒濆嬪寲锛屽垯闇瑕佸厛瑙﹀彂鍏跺垵濮嬪寲銆
銆銆閫氳繃瀛愮被寮曠敤鐖剁被鐨勯潤鎬佸瓧娈碉紝鍙浼氳Е鍙戠埗绫荤殑鍒濆嬪寲锛岃屼笉浼氳Е鍙戝瓙绫荤殑鍒濆嬪寲銆
銆銆瀹氫箟瀵硅薄鏁扮粍锛屼笉浼氳Е鍙戣ョ被鐨勫垵濮嬪寲銆
銆銆甯搁噺鍦ㄧ紪璇戞湡闂翠細瀛樺叆璋冪敤绫荤殑甯搁噺姹犱腑锛屾湰璐ㄤ笂骞舵病鏈夌洿鎺ュ紩鐢ㄥ畾涔夊父閲忕殑绫伙紝涓嶄細瑙﹀彂瀹氫箟甯搁噺鎵鍦ㄧ殑绫汇
銆銆閫氳繃绫诲悕鑾峰彇Class瀵硅薄锛屼笉浼氳Е鍙戠被鐨勫垵濮嬪寲銆
銆銆閫氳繃Class.forName鍔犺浇鎸囧畾绫绘椂锛屽傛灉鎸囧畾鍙傛暟initialize涓篺alse鏃讹紝涔熶笉浼氳Е鍙戠被鍒濆嬪寲锛屽叾瀹炶繖涓鍙傛暟鏄鍛婅瘔铏氭嫙鏈猴紝鏄鍚﹁佸圭被杩涜屽垵濮嬪寲銆
銆銆閫氳繃ClassLoader榛樿ょ殑loadClass鏂规硶锛屼篃涓嶄細瑙﹀彂鍒濆嬪寲鍔ㄤ綔銆
銆銆Java璇瑷绯荤粺鑷甯︿笁涓绫诲姞杞藉櫒:
銆銆铏氭嫙鏈鸿捐″洟闃熸妸鍔犺浇鍔ㄤ綔鏀惧埌JVM澶栭儴瀹炵幇锛屼互渚胯╁簲鐢ㄧ▼搴忓喅瀹氬備綍鑾峰彇鎵闇鐨勭被锛
銆銆路Bootstrap ClassLoader 鏈椤跺眰鐨勫惎鍔ㄧ被鍔犺浇鍣锛屼富瑕佸姞杞芥牳蹇冪被搴擄紝%JRE_HOME%\lib涓嬬殑jar鍖呭拰class鏂囦欢銆傛垨閫氳繃-Xbootclasspath鍙傛暟鎸囧畾璺寰勪腑鐨勭被銆
銆銆路Extention ClassLoader 鎵╁睍鐨勭被鍔犺浇鍣锛屽姞杞界洰褰%JRE_HOME%\lib\ext鐩褰曚笅鐨刯ar鍖呭拰class鏂囦欢銆
銆銆路Appclass Loader搴旂敤绋嬪簭绫诲姞杞藉櫒锛屽姞杞界敤鎴疯矾寰勭殑classpath鐨勬墍鏈夌被銆
銆銆涓変釜绫诲姞杞藉櫒鐨勫姞杞介『搴
銆銆路鍏堝垱寤轰釜椤圭洰锛屽啀鍒涘缓涓猅est.java
java.lang.ClassLoader cl = Test.class.getClassLoader(); System.out.println("ClassLoader is:"+cl.toString()); System.out.println("ClassLoader\'s parent is:"+cl.getParent().toString()); System.out.println("ClassLoader\'s grand father is:"+cl.getParent().getParent().toString());銆銆路鑷宸辩紪鍐欑殑Test.class鏂囦欢鏄鐢盇ppClassLoader鍔犺浇鐨
銆銆路AppClassLoader鐨刾arent鏄疎xtClassLoader锛孍xtClassLoader鐨刾arent鏄痭ull
銆銆路BootStrapClassLoader銆丒xtClassLoader銆丄ppClassLoader閮芥槸鍔犺浇鎸囧畾璺寰勪笅鐨刯ar鍖
銆銆JVM閫氳繃鍙屼翰濮旀淳妯″瀷杩涜岀被鐨勫姞杞斤紝涔熷彲浠ラ氳繃缁ф壙java.lang.ClassLoader瀹炵幇鑷瀹氫箟鐨勭被鍔犺浇鍣ㄣ
銆銆褰撲竴涓绫诲姞杞藉櫒鏀跺埌绫诲姞杞戒换鍔★紝浼氬厛浜ょ粰鍏剁埗绫诲姞杞藉櫒鍘诲畬鎴愶紝鍥犳ゆ渶缁堝姞杞戒换鍔¢兘浼氫紶閫掑埌椤跺眰鐨勫惎鍔ㄧ被鍔犺浇鍣锛屽彧鏈夊綋鐖剁被鍔犺浇鍣ㄦ棤娉曞畬鎴愬姞杞戒换鍔℃椂锛屾墠浼氬皾璇曡嚜宸辨墽琛屽姞杞戒换鍔°傞噰鐢ㄥ弻浜插旀淳鐨勪竴涓濂藉勬槸姣斿傚姞杞戒綅浜巖t.jar鍖呬腑鐨勭被java.lang.Object锛屼笉绠℃槸鍝涓鍔犺浇鍣ㄥ姞杞借繖涓绫伙紝鏈缁堥兘鏄濮旀墭缁欓《灞傜殑鍚鍔ㄧ被鍔犺浇鍣ㄨ繘琛屽姞杞斤紝杩欐牱灏变繚璇佷簡浣跨敤涓嶅悓鐨勭被鍔犺浇鍣ㄦ渶缁堝緱鍒扮殑閮芥槸鍚屾牱涓涓狾bject瀵硅薄銆
銆銆鍔犺浇杩囩▼
銆銆路棣栧厛閫氳繃Class c = findLoadedClass(name);鍒ゆ柇涓涓绫绘槸鍚﹀凡缁忚鍔犺浇杩囥
#p#分页标题#e#銆銆路濡傛灉娌℃湁琚鍔犺浇杩囨墽琛宨f (c == null)涓鐨勭▼搴忥紝閬靛惊鍙屼翰濮旀淳鐨勬ā鍨嬶紝棣栧厛浼氶氳繃閫掑綊浠庣埗鍔犺浇鍣ㄥ紑濮嬫壘锛岀洿鍒扮埗绫诲姞杞藉櫒鏄疊ootstrap ClassLoader涓烘銆
銆銆路鏈鍚庢牴鎹畆esolve鐨勫硷紝鍒ゆ柇杩欎釜class鏄鍚﹂渶瑕佽В鏋愩
銆銆涓涓瀹炰緥
class Singleton{ private static Singleton singleton = new Singleton(); public static int value1; public static int value2 = 0; private Singleton(){ value1++; value2++; } public static Singleton getInstance(){ return singleton; } } class Singleton2{ public static int value1; public static int value2 = 0; private static Singleton2 singleton2 = new Singleton2(); private Singleton2(){ value1++; value2++; } public static Singleton2 getInstance2(){ return singleton2; } }public class jvm {public static void main(String[] args) { Singleton singleton = Singleton.getInstance(); System.out.println("Singleton1 value1:" + singleton.value1); System.out.println("Singleton1 value2:" + singleton.value2); Singleton2 singleton2 = Singleton2.getInstance2(); System.out.println("Singleton2 value1:" + singleton2.value1); System.out.println("Singleton2 value2:" + singleton2.value2); } }銆銆缁撴灉
銆銆Singleton杈撳嚭缁撴灉锛1 0
銆銆棣栧厛鎵ц宮ain涓鐨凷ingleton singleton = Singleton.getInstance();
銆銆鍔犺浇锛氬姞杞界被Singleton
銆銆鍑嗗囷細涓洪潤鎬佸彉閲忓垎閰嶅唴瀛橈紝璁剧疆榛樿ゅ笺傝繖閲屼负singleton(寮曠敤绫诲瀷)璁剧疆涓簄ull,value1,value2(鍩烘湰鏁版嵁绫诲瀷)璁剧疆榛樿ゅ0鍒濆嬪寲(鎸夌収璧嬪艰鍙ヨ繘琛屼慨鏀)锛
銆銆鎵ц宲rivate static Singleton singleton = new Singleton();
銆銆鎵ц孲ingleton鐨勬瀯閫犲櫒锛歷alue1++;value2++; 姝ゆ椂value1锛寁alue2鍧囩瓑浜1
銆銆鎵ц宲ublic static int value1;public static int value2 = 0;
銆銆姝ゆ椂value1=1锛寁alue2=0
銆銆Singleton2杈撳嚭缁撴灉锛1 1
銆銆鎵ц宮ain涓鐨凷ingleton2 singleton2 = Singleton2.getInstance2();
銆銆鍔犺浇锛氬姞杞界被Singleton2
銆銆鍑嗗囷細涓洪潤鎬佸彉閲忓垎閰嶅唴瀛橈紝璁剧疆榛樿ゅ笺傝繖閲屼负value1,value2(鍩烘湰鏁版嵁绫诲瀷)璁剧疆榛樿ゅ0,singleton2(寮曠敤绫诲瀷)璁剧疆涓簄ull,
銆銆鍒濆嬪寲(鎸夌収璧嬪艰鍙ヨ繘琛屼慨鏀)锛
銆銆鎵ц宲ublic static int value2 = 0;
銆銆姝ゆ椂value2=0(value1涓嶅彉锛屼緷鐒舵槸0);
銆銆鎵ц宲rivate static Singleton singleton = new Singleton();
銆銆鎵ц孲ingleton2鐨勬瀯閫犲櫒锛歷alue1++;value2++;
銆銆姝ゆ椂value1锛寁alue2鍧囩瓑浜1,鍗充负鏈鍚庣粨鏋
銆銆鍙傝冩枃鐚锛
銆銆https://blog.csdn.net/briblue/article/details/54973413
銆銆https://yq.aliyun.com/articles/518315
銆銆https://blog.csdn.net/noaman_wgs/article/details/74489549
銆銆
銆銆鍘熸枃浣滆咃細spleated 鏉ユ簮锛氫簯鏍栫ぞ鍖
銆銆鍘熸枃閾炬帴锛歨ttps://yq.aliyun.com/articles/692588?spm=a2c4e.11153940.bloghomeflow.4.2b9f291amLIosJ