Java虛擬機(jī)工作原理JVM_第1頁(yè)
Java虛擬機(jī)工作原理JVM_第2頁(yè)
Java虛擬機(jī)工作原理JVM_第3頁(yè)
Java虛擬機(jī)工作原理JVM_第4頁(yè)
Java虛擬機(jī)工作原理JVM_第5頁(yè)
免費(fèi)預(yù)覽已結(jié)束,剩余19頁(yè)可下載查看

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、AstheJavaVirtualMachineisastack-basedmachine,almostallofitsinstructionsinvolvetheoperandstackinsomeway.Mostinstructionspushvalues,popvalues,orbothastheyperformtheirfunctions.Java虛擬機(jī)是基于棧的(stack-basedmachine)。幾乎所有的java虛擬機(jī)的指令,都與操作數(shù)棧(operandstack)有關(guān).絕大多數(shù)指令都會(huì)在執(zhí)行自己功能的時(shí)候進(jìn)行入棧、出棧操作。1Java體系結(jié)構(gòu)介紹Javasiarchitect

2、urearisesoutoffourdistinctbutinterrelatedtechnologies,eachofwhichisdefinedbyaseparatespecificationfromSunMicrosystems:1.1 Java體系結(jié)構(gòu)包括哪幾部分?Java體系結(jié)本包括4個(gè)獨(dú)立但相關(guān)的技術(shù)theJavaprogramminglanguage程序設(shè)計(jì)語(yǔ)言theJavaclassfileformat字節(jié)碼文件格式theJavaApplicationProgrammingInterface應(yīng)用編程接口theJavaVirtualMachine虛擬機(jī)1.2 什么是JVMjava

3、虛擬機(jī)和javaAPI組成了java運(yùn)行時(shí)。1.3 JVM的主要任務(wù)。Java虛擬機(jī)的主要任務(wù)是裝載class文件并執(zhí)行其中的字節(jié)碼。Java虛擬機(jī)包含了一個(gè)類裝載器。類裝載器的體系結(jié)構(gòu)二種類裝載器啟動(dòng)類裝載器用戶定義的類裝載器啟動(dòng)類裝載器是JVM實(shí)現(xiàn)的一部分當(dāng)被裝載的類引用另外一個(gè)類時(shí),JVM就是使用裝載第一個(gè)類的類裝載器裝載被引用的類。1.4 為什么java容易被反編譯?因?yàn)閖ava程序是動(dòng)態(tài)連接的。從一個(gè)類到另一個(gè)類的引用是符號(hào)化的。在靜態(tài)連接的可執(zhí)行程序中。類之間的引用只是直接的指針或者偏移量。相反在java的class文件中,指向另一個(gè)類的引用通過(guò)字符串清楚的標(biāo)明了所指向的這個(gè)類的名

4、字。如果引用是指向一個(gè)字段的話。這個(gè)字段的名字和描述符(字段的類型)會(huì)被詳細(xì)說(shuō)明。如果引用指向一個(gè)成員方法,那么這個(gè)成員方法的名字和描述符(方法的返回值類型,方法參數(shù)的數(shù)量和類型)會(huì)被詳細(xì)說(shuō)明。包含對(duì)自己字段和成員方法的符號(hào)引用。包含可選的調(diào)試信息。(包括局部變量的名稱和類型)1.5 垃圾回收器缺點(diǎn):無(wú)法確認(rèn)什么時(shí)候開(kāi)始回收垃圾,無(wú)法確認(rèn)是否已經(jīng)開(kāi)始收集,也無(wú)法確認(rèn)要持續(xù)多長(zhǎng)時(shí)間2平臺(tái)無(wú)關(guān)3安全4網(wǎng)絡(luò)移動(dòng)性5Java虛擬機(jī)nativemethodlibrariesruntimedataarcticexecution一,碇加山.engineJ1加)thread1thread2thread3pcr

5、egistersJavastacksLinkVerifyPrepare(oplionally)ResolveInitialize每個(gè)JVM都有一個(gè)類裝載子系統(tǒng)。運(yùn)行時(shí)數(shù)據(jù)區(qū):方法區(qū),堆,java棧,pc寄存器,本地方法棧每個(gè)JVM實(shí)例都有一個(gè)方法區(qū)和堆。M們是由該虛擬機(jī)中所有線程共享的。每個(gè)線程都會(huì)得到自己的pc寄存器和java-,pc寄存器的值指示下一條將被執(zhí)行的指令。java棧記錄存儲(chǔ)該線程中java方法調(diào)用的狀態(tài)。(包括局部變量,參數(shù),返回值,運(yùn)算的中間結(jié)果。)這些內(nèi)存區(qū)域是私有的。任何線程都不能訪問(wèn)另一個(gè)線程的pc寄存器和java棧java棧由許多棧幀組成。一個(gè)棧幀包含一個(gè)java方法

6、的調(diào)用的狀態(tài)。當(dāng)線程調(diào)用一個(gè)方法的時(shí)候,虛擬機(jī)壓入一個(gè)新的棧楨到該線程的java棧中。當(dāng)方法返回時(shí),這個(gè)棧楨被從java棧中彈出并拋棄。引用有3中,類類型,接口類型,數(shù)組類型。JVM中,最基本的數(shù)據(jù)單元是字。上少選擇32位作為字長(zhǎng)。JVM有兩種類裝載器:?jiǎn)?dòng)類裝載器(JVM實(shí)現(xiàn)的一部分,每個(gè)JVM都必須有一用戶自定義的類裝載器(JAVA程序的一部分,必須繼承java.lang.CloassLoade|)。由不同的類裝載器裝載的類被放在虛擬機(jī)內(nèi)部的不同的命名空間中。方法區(qū):大小不固定,根據(jù)需要?jiǎng)討B(tài)調(diào)整方法區(qū)可以被垃圾回收包含提取裝載的類的信息,放到方法區(qū)JVM總能通過(guò)存儲(chǔ)于方法區(qū)的內(nèi)存信息來(lái)確定

7、一個(gè)對(duì)象需要多少內(nèi)存類的靜態(tài)變量也放到方法區(qū)。虛擬機(jī)為裝載的每個(gè)類存儲(chǔ)如下信息:這個(gè)類型的全限定名這個(gè)類型的直接超類的全限定名這個(gè)類型是類類型還是接口類型這個(gè)類的訪問(wèn)權(quán)限修飾符任何直接超接口的全限定名的有序列表該類型的常量池該類型所用常量的一個(gè)有序集合,包括直接常量(String,Integer,floatingpoint),和對(duì)其他類型,字段,方法的符號(hào)引用字段信息字段名字段類型字段的修飾符聲明的順序方法信息方法名方法的返回值類型方法的參數(shù)和類型,順序方法的修飾符方法的操作碼操作數(shù)棧和該方法的棧幀中局部變量區(qū)的大小異常表除了常量以外的所有類(靜態(tài))變量一個(gè)到類CloassLoader的引用一

8、個(gè)到Class類的引用方法表虛擬機(jī)為每一個(gè)裝載的非抽象類都生成一個(gè)方法表一個(gè)java程序獨(dú)占一個(gè)JVM,一個(gè)JVM中只存在一個(gè)堆。|所以,每個(gè)java程序有它自己的堆,但同一個(gè)java程序的1個(gè)線程共享一個(gè)堆運(yùn)行時(shí)創(chuàng)建的所有類實(shí)例數(shù)組對(duì)象垃圾回收器回收內(nèi)存移動(dòng)對(duì)象以減少碎片不必是連續(xù)的內(nèi)存,可以動(dòng)態(tài)的擴(kuò)展和收縮一個(gè)JVM的實(shí)現(xiàn)的方法區(qū)可以在堆頂實(shí)現(xiàn)棧幀棧幀由3部分組成:局部變量區(qū),操作數(shù)棧,幀數(shù)據(jù)區(qū)。局部變量區(qū),操作數(shù)棧的大小在編譯的時(shí)候就確定了。局部變量區(qū)以字長(zhǎng)為單位,從。開(kāi)始計(jì)數(shù)的數(shù)組。int,float,reference,returnaddress只占據(jù)一個(gè)字長(zhǎng)byte,short,c

9、har存入數(shù)組前轉(zhuǎn)換成int,占據(jù)一個(gè)字長(zhǎng)long,double占據(jù)2個(gè)字長(zhǎng)。包含對(duì)應(yīng)方法的參數(shù)和局部變量,方法的局部變量任意決定順序,甚至一個(gè)索引指代兩個(gè)變量,(當(dāng)2個(gè)變量的作用域不重復(fù)時(shí))操作數(shù)棧以字長(zhǎng)為單位的數(shù)組,但不是通過(guò)索引來(lái)訪問(wèn),而是通過(guò)標(biāo)準(zhǔn)的棧操作存貯數(shù)據(jù)的方式和局部變量區(qū)一樣。幀數(shù)據(jù)區(qū)幀數(shù)據(jù)區(qū)保存常量池解析,正常方法返回,異常派發(fā)機(jī)制等信息執(zhí)行引擎線程:JVM只規(guī)定了最高級(jí)別的線程會(huì)得到大多數(shù)的CPU時(shí)間,較低優(yōu)先級(jí)別的線程,只有在所有比它優(yōu)先級(jí)更高的線程全部阻塞的情況下才能保證得到CPU時(shí)間。級(jí)別低的線程在級(jí)別高的線程沒(méi)有被阻塞的時(shí)候,也可能得到CPU時(shí)間,但是這沒(méi)有任何保證

10、。每個(gè)虛擬機(jī)都有一個(gè)主存,用于保存所有的程序變量(對(duì)象的實(shí)例變量,數(shù)組的元素,以及類變量)。每一個(gè)線程都有一個(gè)工作內(nèi)存,線程用它保存所使用和賦值的“工作拷貝”。局部變量和參數(shù),因?yàn)樗麄兪敲總€(gè)線程私有的,可以邏輯上看成是工作內(nèi)存或者主存的一部分。6字節(jié)碼文件8位字節(jié)的二進(jìn)制流classFile表的格式Type	NameCountu4magic1u2minor_version1u2major_version1u2constant_pool_count1cp_infoconstant_poolconstant_pool_count-1u2access_flags1u2this_clas

11、s1u2super_class1u2interfaces_count1u2interfacesinterfaces_countu2fields_count1field_infofieldsfields_countu2methodscount1method_infomethodsmethods_countu2attributes_count1attribute_infoattributesattributes_countConstantpooltags常量池標(biāo)志每一個(gè)標(biāo)志都有一個(gè)相對(duì)應(yīng)的表。表名通過(guò)在標(biāo)志后面加上"info"后綴來(lái)產(chǎn)生EntryTypeTagValueDesc

12、riptionCONSTANT_Utf81AUTF-8encodedUnicodestringCONSTANT_Integer3AnintliteralvalueCONSTANT_Float4AfloatliteralvalueCONSTANT_Long5AlongliteralvalueCONSTANT_Double6AdoubleliteralvalueCONSTANT_Class7AsymbolicreferencetoaclassorinterfaceCONSTANT_String8AStringliteralvalueCONSTANT_Fieldref9Asymbolicrefere

13、ncetoafieldCONSTANT_Methodref10AsymbolicreferencetoamethoddeclaredinaclassCONSTANT_InterfaceMethodref11AsymbolicreferencetoamethoddeclaredinaninterfaceCONSTANT_NameAndType12Partofasymbolicreferencetoafieldormethod7類型的聲明周期裝載通過(guò)該類型的完全限定名,產(chǎn)生一個(gè)該類型的二進(jìn)制數(shù)據(jù)流解析這個(gè)二進(jìn)制數(shù)據(jù)流為方法區(qū)內(nèi)的內(nèi)部數(shù)據(jù)結(jié)構(gòu)(并在堆上)創(chuàng)建一個(gè)表示該類型的java.lang.Cla

14、ss類的實(shí)例連接(已讀入的二進(jìn)制形式的類型數(shù)據(jù)合并到虛擬機(jī)的運(yùn)行時(shí)狀態(tài)中去)驗(yàn)證(保證java類型數(shù)據(jù)格式正確并適合JVM使用)準(zhǔn)備(分配內(nèi)存,默認(rèn)初始化在此時(shí)發(fā)生)解析(把常量池中的符號(hào)引用(類,接口,字段,方法)轉(zhuǎn)換為直接引用,虛擬機(jī)的實(shí)現(xiàn)可以推遲解析這一步,它可以在當(dāng)運(yùn)行中的程序真正使用某個(gè)符號(hào)引用是再去解析它)初始化(將類變量賦予適當(dāng)?shù)某跏贾担@式初始化),所有JVM的實(shí)現(xiàn)必須在每個(gè)類或接口首次主動(dòng)使用是被初始化)對(duì)象的生命周期類實(shí)例化有四種途徑:new調(diào)用Class或者java.lang.reflect.Constructor的newInstance()clone()java.io.

15、ObjectInputStream的getObject()8連接模型動(dòng)態(tài)連接和解析常量池:class文件把它所有的引用符號(hào)保存在一個(gè)地方,常量池每個(gè)文件有一個(gè)常量池每一個(gè)被JVM裝載的類或者接口都有一份內(nèi)部版本的常量池,被稱作運(yùn)行時(shí)常量池運(yùn)行時(shí)常量池映射到class文件的常量池JVM為每一個(gè)裝載的類和接口保存一份獨(dú)立的常量池。來(lái)自相同方法或不同方法中的幾條指令,可能指向同一個(gè)常量池入口。每個(gè)常量池入口只被解析一次。解析在程序運(yùn)行的某些時(shí)刻,如果某個(gè)特定的符號(hào)引用將要被使用,它首先要被解析。解析過(guò)程就是根據(jù)符號(hào)引用查找到實(shí)體,在把符號(hào)引用替換成直接引用的過(guò)程。所有的符號(hào)引用都保持在常量池,所以這

16、個(gè)過(guò)程也被稱作常量池解析。解析分為早解析和遲解析。9垃圾收集10棧和局部變量操作StackandLocalVariableOperations10.1 常量入棧操作PushingConstantsOntotheStack10.1.1 將一個(gè)字長(zhǎng)的常量壓入棧OpcodeOperand(s)Descriptioniconst_m1(none)pushesint-1ontothestackiconst_0(none)pushesint0ontothestackiconst_1(none)pushesint1ontothestackiconst_2(none)pushesint2ontothestac

17、kiconst_3(none)pushesint3ontothestackiconst_4(none)pushesint4ontothestackiconst_5(none)pushesint5ontothestackfconst_0(none)pushesfloat0ontothestackfconst_1(none)pushesfloat1ontothestackfconst_2(none)pushesfloat2ontothestack10.1.2 將一個(gè)字長(zhǎng)的常量壓入棧longanddoublevaluesoccupy64bits.Eachtimealongordoubleispush

18、edontothestack,itsvalueoccupiestwoslotsonthestack.long和double類型的值是64位長(zhǎng)度的值,每當(dāng)一個(gè)long或者double類型的值被壓入棧,將占據(jù)2個(gè)位置OpcodeOperand(s)Descriptionlconst_0(none)pusheslong0ontothestacklconst_1(none)pusheslong1ontothestackdconst_0(none)pushesdouble0ontothestackdconst_1(none)pushesdouble1ontothestack10.1.3 將空的對(duì)象引用(

19、null)壓入棧opcode,Oneotheropcodepushesanimplicitconstantvalueontothestack.Theaconst_nullpushesanullobjectreferenceontothestack.OpcodeOperand(s)Descriptionaconst_null(none)pushesanullobjectreferenceontothestack10.1.4 將byte和short類型常量壓入棧PushingbyteandshortconstantsontothestackOpcodeOperand(s)Descriptionbi

20、pushbyte1expandsbyte1(abytetype)toanintandpushesitontothestack將byte1(數(shù)據(jù)典型為byte)轉(zhuǎn)換為int然后將其壓入棧sipushbyte1,byte2expandsbyte1,byte2(ashorttype)toanintandpushesitontothestack將byte1和byte2(數(shù)據(jù)典型為short)轉(zhuǎn)換為int然后將其壓入棧10.1.5 將常量池入口壓入棧PushingconstantpoolentriesontothestackOpcodeOperand(s)Descriptionldcindexbyte1

21、pushessingle-wordvaluefromconstantpoolentryspecifiedbyindexbyte1ontothestackldc_windexbyte1,pushessingle-wordvaluefromconstantpoolentryindexbyte2specifiedbyindexbyte1,indexbyte2ontothestackldc2_windexbyte1,pushesdual-wordvaluefromconstantpoolentryindexbyte2specifiedbyindexbyte1,indexbyte2ontothestac

22、k10.2 通用棧操作GenericStackOperations10.2.1 棧操作StackmanipulationOpcodeOperand(s)Descriptionnop(none)donothingpop(none)popthetopwordfromtheoperandstackpop2(none)popthetoptwowordsfromtheoperandstackswap(none)swapthetopoperandstacktwowordsdup(none)duplicatetopoperandstackworddup2(none)duplicatetoptwooperan

23、dstackwordsdup_x1(none)duplicatetopoperandstackwordandputtwodowndup_x2(none)duplicatetopoperandstackwordandputthreedowndup2_x1(none)duplicatetoptwooperandstackwordsandputthreedowndup2_x2(none)duplicatetoptwooperandstackwordsandputfourdown10.3 把局部變量壓入棧PushingLocalVariablesOntotheStack10.3.1 將1個(gè)字長(zhǎng)的局部變

24、量壓入棧Pushingsingle-wordlocalvariablesontothestackOpcodeOperand(s)Descriptioniloadvindexpushesintfromlocalvariablepositionvindexiload_0(none)pushesintfromlocalvariablepositionzeroiload_1(none)pushesintfromlocalvariablepositiononeiload_2(none)pushesintfromlocalvariablepositiontwoiload_3(none)pushesintf

25、romlocalvariablepositionthreefloadvindexpushesfloatfromlocalvariablepositionvindexfload_0(none)pushesfloatfromlocalvariablepositionzerofload_1(none)pushesfloatfromlocalvariablepositiononefload_2(none)pushesfloatfromlocalvariablepositiontwofload_3(none)pushesfloatfromlocalvariablepositionthree10.3.2

26、將2個(gè)字長(zhǎng)的局部變量壓入棧Pushingdual-wordlocalvariablesontothestackOpcodeOperand(s)Descriptionlloadvindexpusheslongfromlocalvariablepositionsvindexand(vindex+1)lload_0(none)pusheslongfromlocalvariablepositionszeroandonelload_1(none)pusheslongfromlocalvariablepositionsoneandtwolload_2(none)pusheslongfromlocalvar

27、iablepositionstwoandthreelload_3(none)pusheslongfromlocalvariablepositionsthreeandfourdloadvindexpushesdoublefromlocalvariablepositionsvindexand(vindex+1)dload_0(none)pushesdoublefromlocalvariablepositionszeroandonedload_1(none)pushesdoublefromlocalvariablepositionsoneandtwodload_2(none)dload_3(none

28、)pushesdoublefromlocalvariablepositionstwoandthreepushesdoublefromlocalvariablepositionsthreeandfour10.3.3 將對(duì)象引用局部變量壓入棧Table10-9.PushingobjectreferencelocalvariablesontothestackOpcodeOperand(s)Descriptionaloadvindexpushesobjectreferencefromlocalvariablepositionvindexaload_0(none)pushesobjectreferenc

29、efromlocalvariablepositionzeroaload_1(none)pushesobjectreferencefromlocalvariablepositiononeaload_2(none)pushesobjectreferencefromlocalvariablepositiontwoaload_3(none)pushesobjectreferencefromlocalvariablepositionthree10.4 彈由棧頂部元素,將其賦給局部變量PoppingtoLocalVariables10.4.1 彈由一個(gè)字長(zhǎng)的值,將其賦給局部變量Poppingsingle-

30、wordvaluesintolocalvariablesOpcodeOperand(s)Descriptionistorevindexpopsinttolocalvariablepositionvindexistore_0(none)popsinttolocalvariablepositionzeroistore_1(none)popsinttolocalvariablepositiononeistore_2(none)popsinttolocalvariablepositiontwoistore_3(none)popsinttolocalvariablepositionthreefstore

31、vindexpopsfloattolocalvariablepositionvindexfstore_0(none)popsfloattolocalvariablepositionzerofstore_1(none)popsfloattolocalvariablepositiononefstore_2(none)popsfloattolocalvariablepositiontwofstore_3(none)popsfloattolocalvariablepositionthree10.4.2 彈由2個(gè)字長(zhǎng)的值,將其賦給局部變量Poppingdual-wordvaluesintolocalva

32、riablesOpcodeOperand(s)Descriptionlstorevindexpopslongtolocalvariablepositionsvindexand(vindex+1)lstore_0(none)popslongtolocalvariablepositionszeroandonelstore_1(none)popslongtolocalvariablepositionsoneandtwolstore_2(none)popslongtolocalvariablepositionstwoandthreelstore_3(none)popslongtolocalvariab

33、lepositionsthreeandfourdstorevindexpopsdoubletolocalvariablepositionsvindexand(vindex+1)dstore_0(none)popsdoubletolocalvariablepositionszeroandonedstore_1(none)popsdoubletolocalvariablepositionsoneandtwodstore_2(none)popsdoubletolocalvariablepositionstwoandthreedstore_3(none)popsdoubletolocalvariabl

34、epositionsthreeandfour10.4.3 彈由對(duì)象引用,將其賦給局部變量PoppingobjectreferencesintolocalvariablesOpcodeOperand(s)Descriptionastorevindexpopsobjectreferencetolocalvariablepositionvindexastore_0(none)popsobjectreferencetolocalvariablepositionzeroastore_1(none)popsobjectreferencetolocalvariablepositiononeastore_2(

35、none)popsobjectreferencetolocalvariablepositiontwoastore_3(none)popsobjectreferencetolocalvariablepositionthree10.5 wide指令ThewideInstruction10.5.1 彈由對(duì)象引用,將其賦給局部變量Poppingobjectreferencesintolocalvariables無(wú)符號(hào)8位局部變量索引,把方法中的局部變量限制在256以下。一條單獨(dú)的wide指令,可將8位的索引再擴(kuò)展8位。這樣就可以把局部變量的限制擴(kuò)展到65536OpcodeOperand(s)Descr

36、iptionwideiload,indexbyte2indexbyte1,pushesintfromlocalvariablepositionindexwidelload,indexbyte2indexbyte1,pusheslongfromlocalvariablepositionindexwidefload,indexbyte2indexbyte1,pushesfloatfromlocalvariablepositionindexwidedload,indexbyte1,pushesdoublefromlocalvariablepositionindexbyte2indexwidealoa

37、d,indexbyte1,pushesobjectreferencefromlocalvariableindexbyte2positionindexwideistore,indexbyte1,popsinttolocalvariablepositionvindexindexbyte2widelstore,indexbyte2indexbyte1,popslongtolocalvariablepositionindexwidefstore,indexbyte2indexbyte1,popsfloattolocalvariablepositionindexwidedstore,indexbyte2

38、indexbyte1,popsdoubletolocalvariablepositionindexwideastore,indexbyte1,popsobjectreferencetolocalvariablepositionindexbyte2index11類型轉(zhuǎn)換TypeConversion11.1 longs,floats,anddoubles類型之間的轉(zhuǎn)換Convertingints,longs,floats,anddoublesOpcodeOperand(s)Descriptioni2l(none)convertsinttolongi2f(none)convertsinttofloa

39、ti2d(none)convertsinttodoublel2i(none)convertslongtointl2f(none)convertslongtofloatl2d(none)convertslongtodoublef2i(none)convertsfloattointf2l(none)convertsfloattolongf2d(none)convertsfloattodoubled2i(none)convertsdoubletointd2l(none)convertsdoubletolongd2f(none)convertsdoubletofloat11.2 int數(shù)據(jù)類型向byt

40、e,char,short類型的轉(zhuǎn)換Convertingints,bytes,chars,andshorts.Noopcodesexistthatconvertdirectlyfromalong,float,ordoubletothetypessmallerthanintJava虛擬機(jī)中沒(méi)有把long,float,ordouble類型值直接轉(zhuǎn)換成比int類型占據(jù)更小的空間的數(shù)據(jù)類型的操作碼。Thereforeconvertingfromafloattoabyte,forexample,requirestwosteps.Firstthefloatmustbeconvertedtoanintwith

41、f2i,thentheresultingintcanbeconvertedtoabytewithi2b.因此,把float類型值轉(zhuǎn)換為byte類型需要兩個(gè)步驟,首先,float類型值必須通過(guò)指令轉(zhuǎn)換為int類型值,然后,所得的int值,再通過(guò)i2b指令轉(zhuǎn)換為byte類型值A(chǔ)lthoughopcodesexistthatconvertaninttoprimitivetypessmallerthanint(byte,short,andchar),noopcodesexistthatconvertintheoppositedirection.Thisisbecauseanybytes,shorts,

42、orcharsareeffectivelyconvertedtointbeforebeingpushedontothestack.盡管有操作碼可以把int類型的值轉(zhuǎn)換為比int類型值占據(jù)更小空間的數(shù)據(jù)類型(byte,short,andchar),但并不存在執(zhí)行相反方向轉(zhuǎn)換操作的操作碼,因?yàn)槿魏蝏yte,short,andchar類型值在壓入棧的時(shí)候,就已經(jīng)有效的被轉(zhuǎn)換成int類型值了。Arithmeticoperationsuponbytes,shorts,andcharsaredonebyfirstconvertingthevaluestoint,performingthearithmeti

43、coperationsontheints,andbeinghappywithanintresult.涉及bytes,shorts,andchars類型的運(yùn)算操作首先會(huì)把這些值轉(zhuǎn)換成int類型,然后對(duì)int類型的值進(jìn)行計(jì)算,最后得到int類型的結(jié)果。OpcodeOperand(s)Descriptioni2b(none)convertsinttobytei2c(none)convertsinttochari2s(none)convertsinttoshort12整數(shù)運(yùn)算IntegerArithmetictwo'scomplement補(bǔ)碼Allintegertypessupportedby

44、theJavaVirtualMachine-bytes,shorts,ints,andlongs-aresignedtwo's-complementnumbers.Java虛擬機(jī)支持的所有整數(shù)類型,bytes,shorts,ints,andlongs,他們都是帶符號(hào)的二進(jìn)制補(bǔ)碼數(shù)。Thetwo's-complementschemeallowsbothpositiveandnegativeintegerstoberepresented.二進(jìn)制補(bǔ)碼方案,既能描述正整數(shù),又能描述負(fù)整數(shù)Themostsignificantbitofatwo's-complementnumber

45、isitssignbit.Thesignbitisonefornegativenumbersandzeroforpositivenumbersandforthenumberzero.符號(hào)位為1表示負(fù)整數(shù),符號(hào)為0表示表示正整數(shù)和數(shù)字0Thenumberofuniquevaluesthatcanberepresentedbythetwo's-complementschemeistworaisedtothepowerofthetotalnumberofbits能被二進(jìn)制補(bǔ)碼方案表示的數(shù)的范圍為:2的總位數(shù)次募Forexample,theshorttypeinJavaisa16-bitsig

46、nedtwo's-complementinteger.Thenumberofuniqueintegersthatcanberepresentedbythisschemeis216,or65,536.例如,short在java中是16位的帶符號(hào)的二進(jìn)制補(bǔ)碼整數(shù),能夠唯一表示的整數(shù)為216,或者65,536Halfoftheshorttype'srangeofvaluesareusedtorepresentzeroandpositivenumbers;theotherhalfoftheshorttype'srangeareusedtorepresentnegativenum

47、bers.Short類型值范圍的一半用來(lái)表示0和正整數(shù),另一般用來(lái)表示負(fù)整數(shù)。Therangeofnegativevaluesfora16-bittwo's-complementnumberis-32,768(0x8000)to-1(0xffff).Zerois0x0000.Therangeofpositivevaluesisone(0x0001)to32,767(0x7fff).16位2進(jìn)制補(bǔ)碼負(fù)數(shù)的范圍是-32,768(0x8000)to-1(0xffff).0用0x0000來(lái)表示.正整數(shù)的范圍是(0x0001)to32,767(0x7fff).Positivenumbersare

48、intuitiveinthattheyaremerelythebasetworepresentationofthenumber.Negativenumberscanbecalculatedbyaddingthenegativenumbertotworaisedtothepowerofthetotalnumberofbits.整數(shù)直覺(jué)上只不過(guò)是數(shù)的兩種表示法之一。負(fù)數(shù)可以通過(guò)負(fù)數(shù)和2的某次方哥相加而得出。Forexample,thetotalnumberofbitsinashortis16,sothetwo's-complementrepresentationofanegativenum

49、berinthevalidrangeforashort(-32,768to-1)canbecalculatedbyaddingthenegativenumberto216,or65,536.Thetwo's-complementrepresentationfor-1is65,536+(-1)or65,535(0xffff).Thetwo's-complementrepresentationfor-2is65,536+(-2)or65,534(0xfffe).例如,short類型的長(zhǎng)度為16位。因此2進(jìn)制補(bǔ)碼表示法可以通過(guò)一個(gè)負(fù)數(shù)和2的16次哥的相加來(lái)得到一個(gè)有效范圍內(nèi)的負(fù)數(shù)。-

50、1的二進(jìn)制補(bǔ)碼表示為65,536+(-1)or65,535(0xffff).-2的二進(jìn)制補(bǔ)碼表示為65,536+(-2)or65,534(0xfffe).Additionisperformedontwo's-complementsignednumbersinthesamewayitwouldbeperformedonunsignedbinarynumbers.Thetwonumbersareadded,overflowisignored,andtheresultisinterpretedasasignedtwo's-complementnumber.Thiswillworkas

51、longastheresultisactuallywithintherangeofvalidvaluesforthetype.Forexample,toadd4+(-2),justadd0x00000004and0xfffffffe.Theresultisactually0x100000002,butbecausethereareonly32bitsinanint,theoverflowisignoredandtheresultbecomes0x00000002.在帶符號(hào)的二進(jìn)制補(bǔ)碼數(shù)上進(jìn)行加法運(yùn)算,與在無(wú)符號(hào)二進(jìn)制數(shù)上進(jìn)行加法運(yùn)算一樣。兩個(gè)數(shù)相加(忽略溢出),結(jié)果被解釋為一個(gè)帶符號(hào)的二進(jìn)制補(bǔ)

52、碼數(shù)。這個(gè)過(guò)程將在運(yùn)算結(jié)果是該類型的有效范圍內(nèi)的情況下運(yùn)行。例如要獲得4+(-2)的結(jié)果,只要把0x00000004and0xfffffffe相加即可.結(jié)果是0x100000002,但因?yàn)閕nt類型只有32位,于是溢出部分被忽略,結(jié)果為0x00000002.0000000000000000000000000000010011111111111111111111111111111110+10000000000000000000000000000010OverflowinintegeroperationsdoesnotthrowanyexceptionintheJavaVirtualMachine

53、.Theresultismerelytruncatedtofitintotheresulttype(eitherintorlong).Forexample,addingints0x7ffffffand1yields0x80000000.ThismeansthattheJavaVirtualMachinewillreportthat2,147,483,647+1=-2,147,483,648,ifthetypeofthevaluesbeingaddedareintsandnotlongs.AsyouprograminJava,youmustkeepinmindthatoverflowcanhap

54、penandmakesureyouchoosetheappropriatetype,intorlong,ineachsituation.IntegerdivisionbyzerodoesthrowanArithmeticException,soyoushouldalsokeepinmindthatthisexceptioncouldbethrownandcatchitifnecessary.Java虛擬機(jī)中整數(shù)運(yùn)算的溢出并不會(huì)導(dǎo)致拋出異常。其結(jié)果只被簡(jiǎn)單的截?cái)嘁苑蠑?shù)據(jù)類型(或者為int,或者為long)。例如把int值0x7ffffff和1相加,將會(huì)得到0x80000000.因此如果相加的值的類型為int而非long,java虛擬機(jī)中2,147,483,647+1的結(jié)果將是-2,147,483,648。在java中編程時(shí),你必須隨時(shí)注意可能發(fā)生的溢出,必須在每種情況下確認(rèn)所選擇的數(shù)據(jù)類型(intorlong)是否正確。整數(shù)被0除會(huì)拋出一個(gè)ArithmeticException異常,所以應(yīng)該時(shí)刻牢記此類異常將會(huì)拋出,必須在必要的時(shí)候捕獲異常。Ifyouencounterasituationinwhichlongjustisn'tlongenou

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論