Java编程思想毕业论文外文文献翻译.docx
- 文档编号:18044876
- 上传时间:2023-08-07
- 格式:DOCX
- 页数:18
- 大小:60.74KB
Java编程思想毕业论文外文文献翻译.docx
《Java编程思想毕业论文外文文献翻译.docx》由会员分享,可在线阅读,更多相关《Java编程思想毕业论文外文文献翻译.docx(18页珍藏版)》请在冰点文库上搜索。
Java编程思想毕业论文外文文献翻译
ThinkinginJava
AlthoughitisbasedonC++,Javaismoreofa“pure”object-orientedlanguage.BothC++andJavaarehybridlanguages,butinJavathedesignersfeltthatthehybridizationwasnotasimportantasitwasinC++.Ahybridlanguageallowsmultipleprogrammingstyles;thereasonC++ishybridistosupportbackwardcompatibilitywiththeClanguage.BecauseC++isasupersetoftheClanguage,itincludesmanyofthatlanguage’sundesirablefeatures,whichcanmakesomeaspectsofC++overlycomplicated.TheJavalanguageassumesthatyouwanttodoonlyobject-orientedprogramming.Thismeansthatbeforeyoucanbeginyoumustshiftyourmindsetintoanobject-orientedworld(unlessit’salreadythere).ThebenefitofthisinitialeffortistheabilitytoprograminalanguagethatissimplertolearnandtousethanmanyotherOOPlanguages.Inthischapterwe’llseethebasiccomponentsofaJavaprogramandwe’lllearnthateverythinginJavaisanobject,evenaJavaprogram.
Eachprogramminglanguagehasitsownmeansofmanipulatingdata.Sometimestheprogrammermustbeconstantlyawareofwhattypeofmanipulationisgoingon.Areyoumanipulatingtheobjectdirectly,orareyoudealingwithsomekindofindirectrepresentation(apointerinCorC++)thatmustbetreatedwithaspecialsyntax?
AllthisissimplifiedinJava.Youtreateverythingasanobject,usingasingleconsistentsyntax.Althoughyoutreateverythingasanobject,theidentifieryoumanipulateisactuallya“reference”toanobject.Youmightimaginethissceneasatelevision(theobject)withyourremotecontrol(thereference).Aslongasyou’reholdingthisreference,youhaveaconnectiontothetelevision,butwhensomeonesays“changethechannel”or“lowerthevolume,”whatyou’remanipulatingisthereference,whichinturnmodifiestheobject.Ifyouwanttomovearoundtheroomandstillcontrolthetelevision,youtaketheremote/referencewithyou,notthetelevision.
Also,theremotecontrolcanstandonitsown,withnotelevision.Thatis,justbecauseyouhaveareferencedoesn’tmeanthere’snecessarilyanobjectconnectedtoit.Soifyouwanttoholdawordorsentence,youcreateaStringreference:
Buthereyou’vecreatedonlythereference,notanobject.Ifyoudecidedtosendamessagetosatthispoint,you’llgetanerror(atruntime)becausesisn’tactuallyattachedtoanything(there’snotelevision).Asaferpractice,then,isalwaystoinitializeareferencewhenyoucreateit.
However,thisusesaspecialJavafeature:
stringscanbeinitializedwithquotedtext.Normally,youmustuseamoregeneraltypeofinitializationforobjects
Whenyoucreateareference,youwanttoconnectitwithanewobject.Youdoso,ingeneral,withthenewkeyword.Thekeywordnewsays,“Makemeanewoneoftheseobjects.”Sointheprecedingexample,youcansay:
Notonlydoesthismean“MakemeanewString,”butitalsogivesinformationabouthowtomaketheStringbysupplyinganinitialcharacterstring.
Ofcourse,Stringisnottheonlytypethatexists.Javacomeswithaplethoraofready-madetypes.What’smoreimportantisthatyoucancreateyourowntypes.Infact,that’sthefundamentalactivityinJavaprogramming,andit’swhatyou’llbelearningaboutintherestofthisbook
It’susefultovisualizesomeaspectsofhowthingsarelaidoutwhiletheprogramisrunning—inparticularhowmemoryisarranged.Therearesixdifferentplacestostoredata:
Registers.Thisisthefasteststoragebecauseitexistsinaplacedifferentfromthatofotherstorage:
insidetheprocessor.However,thenumberofregistersisseverelylimited,soregistersareallocatedbythecompileraccordingtoitsneeds.Youdon’thavedirectcontrol,nordoyouseeanyevidenceinyourprogramsthatregistersevenexist.
Thestack.Thislivesinthegeneralrandom-accessmemory(RAM)area,buthasdirectsupportfromtheprocessorviaitsstackpointer.Thestackpointerismoveddowntocreatenewmemoryandmoveduptoreleasethatmemory.Thisisanextremelyfastandefficientwaytoallocatestorage,secondonlytoregisters.TheJavacompilermustknow,whileitiscreatingtheprogram,theexactsizeandlifetimeofallthedatathatisstoredonthestack,becauseitmustgeneratethecodetomovethestackpointerupanddown.Thisconstraintplaceslimitsontheflexibilityofyourprograms,sowhilesomeJavastorageexistsonthestack—inparticular,objectreferences—Javaobjectsthemselvesarenotplacedonthestack.
Theheap.Thisisageneral-purposepoolofmemory(alsointheRAMarea)whereallJavaobjectslive.Thenicethingabouttheheapisthat,unlikethestack,thecompilerdoesn’tneedtoknowhowmuchstorageitneedstoallocatefromtheheaporhowlongthatstoragemuststayontheheap.Thus,there’sagreatdealofflexibilityinusingstorageontheheap.Wheneveryouneedtocreateanobject,yousimplywritethecodetocreateitbyusingnew,andthestorageisallocatedontheheapwhenthatcodeisexecuted.Ofcoursethere’sapriceyoupayforthisflexibility.Ittakesmoretimetoallocateheapstoragethanitdoestoallocatestackstorage(ifyouevencouldcreateobjectsonthestackinJava,asyoucaninC++).
Staticstorage.“Static”isusedhereinthesenseof“inafixedlocation”(althoughit’salsoin
RAM).Staticstoragecontainsdatathatisavailablefortheentiretimeaprogramisrunning.Youcanusethestatickeywordtospecifythataparticularelementofanobjectisstatic,butJavaobjectsthemselvesareneverplacedinstaticstorage.
Constantstorage.Constantvaluesareoftenplaceddirectlyintheprogramcode,whichissafesincetheycanneverchange.Sometimesconstantsarecordonedoffbythemselvessothattheycanbeoptionallyplacedinread-onlymemory(ROM),inembeddedsystems.
Non-RAMstorage.Ifdatalivescompletelyoutsideaprogram,itcanexistwhiletheprogramisnotrunning,outsidethecontroloftheprogram.Thetwoprimaryexamplesofthisarestreamedobjects,inwhichobjectsareturnedintostreamsofbytes,generallytobesenttoanothermachine,andpersistentobjects,inwhichtheobjectsareplacedondisksotheywillholdtheirstateevenwhentheprogramisterminated.Thetrickwiththesetypesofstorageisturningtheobjectsintosomethingthatcanexistontheothermedium,andyetcanberesurrectedintoaregularRAM-basedobjectwhennecessary.Javaprovidessupportforlightweightpersistence,andfutureversionsofJavamightprovidemorecompletesolutionsforpersistence
Onegroupoftypes,whichyou’llusequiteofteninyourprogramming,getsspecialtreatment.Youcanthinkoftheseas“primitive”types.Thereasonforthespecialtreatmentisthattocreateanobjectwithnew—especiallyasmall,simplevariable—isn’tveryefficient,becausenewplacesobjectsontheheap.ForthesetypesJavafallsbackontheapproachtakenbyCandC++.Thatis,insteadofcreatingthevariablebyusingnew,an“automatic”variableiscreatedthatisnotareference.Thevariableholdsthevalue,andit’splacedonthestack,soit’smuchmoreefficient.
Javadeterminesthesizeofeachprimitivetype.Thesesizesdon’tchangefromonemachinearchitecturetoanotherastheydoinmostlanguages.ThissizeinvarianceisonereasonJavaprogramsareportable
Allnumerictypesaresigned,sodon’tlookforunsignedtypes.
Thesizeofthebooleantypeisnotexplicitlyspecified;itisonlydefinedtobeabletotaketheliteralvaluestrueorfalse.
The“wrapper”classesfortheprimitivedatatypesallowyoutomakeanonprimitiveobjectontheheaptorepresentthatprimitivetype.Forexample:
charc='x';
CharacterC=newCharacter(c);
Oryoucouldalsouse:
CharacterC=newCharacter('x');
Thereasonsfordoingthiswillbeshowninalaterchapter.
Javaincludestwoclassesforperforminghigh-precisionarithmetic:
BigIntegerandBigDecimal.Althoughtheseapproximatelyfitintothesamecategoryasthe“wrapper”classes,neitheronehasaprimitiveanalogue.
Bothclasseshavemethodsthatprovideanaloguesfortheoperationsthatyouperformonprimitivetypes.Thatis,youcandoanythingwithaBigIntegerorBigDecimalthatyoucanwithanintorfloat,it’sjustthatyoumustusemethodcallsinsteadofoperators.Also,sincethere’smoreinvolved,theoperationswillbeslower.You’reexchangingspeedforaccuracy.
BigIntegersupportsarbitrary-precisionintegers.Thismeansthatyoucanaccuratelyrepresentintegralvaluesofanysizewithoutlosinganyinformationduringoperations.
BigDecimalisforarbitrary-precisionfixed-pointnumbers;youcanusetheseforaccuratemonetarycalculations,forexample.
ConsulttheJDKdocumentationfordetailsabouttheconstructorsandmethodsyoucancallforthesetwoclasses
Virtuallyallprogramminglanguagessupportarrays.UsingarraysinCandC++isperilousbecausethosearraysareonlyblocksofmemory.Ifaprogramaccessesthearrayoutsideofitsmemoryblockorusesthememorybeforeinitialization(commonprogrammingerrors),therewillbeunpredictableresults.
OneoftheprimarygoalsofJavaissafety,somanyoftheproblemsthatplagueprogrammersinCandC++arenotrepeatedinJava.AJavaarrayisguaranteedtobeinitializedandcannotbeaccessedoutsideofitsrange.Therangecheckingcomesatthepriceofhavingasmallamountofmemoryoverheadoneacharrayaswellasverifyingtheindexatruntime,buttheassumptionisthatthesafetyandincreasedproductivityisworththeexpense.
Whenyoucreateanarrayofobjects,youarereallycreatinganarrayofreferences,andeachofthosereferencesisautomaticallyinitializedtoaspec
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Java 编程 思想 毕业论文 外文 文献 翻译
![提示](https://static.bingdoc.com/images/bang_tan.gif)