计算机专业文献翻译在 JDK 早期版本中使用 Java 5 的语言特性Word文档格式.docx

上传人:b****3 文档编号:6477596 上传时间:2023-05-06 格式:DOCX 页数:11 大小:23.40KB
下载 相关 举报
计算机专业文献翻译在 JDK 早期版本中使用 Java 5 的语言特性Word文档格式.docx_第1页
第1页 / 共11页
计算机专业文献翻译在 JDK 早期版本中使用 Java 5 的语言特性Word文档格式.docx_第2页
第2页 / 共11页
计算机专业文献翻译在 JDK 早期版本中使用 Java 5 的语言特性Word文档格式.docx_第3页
第3页 / 共11页
计算机专业文献翻译在 JDK 早期版本中使用 Java 5 的语言特性Word文档格式.docx_第4页
第4页 / 共11页
计算机专业文献翻译在 JDK 早期版本中使用 Java 5 的语言特性Word文档格式.docx_第5页
第5页 / 共11页
计算机专业文献翻译在 JDK 早期版本中使用 Java 5 的语言特性Word文档格式.docx_第6页
第6页 / 共11页
计算机专业文献翻译在 JDK 早期版本中使用 Java 5 的语言特性Word文档格式.docx_第7页
第7页 / 共11页
计算机专业文献翻译在 JDK 早期版本中使用 Java 5 的语言特性Word文档格式.docx_第8页
第8页 / 共11页
计算机专业文献翻译在 JDK 早期版本中使用 Java 5 的语言特性Word文档格式.docx_第9页
第9页 / 共11页
计算机专业文献翻译在 JDK 早期版本中使用 Java 5 的语言特性Word文档格式.docx_第10页
第10页 / 共11页
计算机专业文献翻译在 JDK 早期版本中使用 Java 5 的语言特性Word文档格式.docx_第11页
第11页 / 共11页
亲,该文档总共11页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

计算机专业文献翻译在 JDK 早期版本中使用 Java 5 的语言特性Word文档格式.docx

《计算机专业文献翻译在 JDK 早期版本中使用 Java 5 的语言特性Word文档格式.docx》由会员分享,可在线阅读,更多相关《计算机专业文献翻译在 JDK 早期版本中使用 Java 5 的语言特性Word文档格式.docx(11页珍藏版)》请在冰点文库上搜索。

计算机专业文献翻译在 JDK 早期版本中使用 Java 5 的语言特性Word文档格式.docx

Butevennow,whenIaskdeveloperswhichversionoftheJavaplatformtheyareusingindevelopment,typicallyonlyhalfareusingJava5--andtheotherhalfarejealous.TheyareeagertousethelanguagefeaturesaddedinJava5suchasgenericsandannotations,butanumberoffactorsstillpreventmanyfromdoingso.

OnecategoryofdevelopersunabletotakeadvantageofJava5featuresarethosewhodevelopcomponents,libraries,orapplicationframeworks.BecausetheircustomersmaystillbeusingJDK1.4orearlierandclassescompiledwithJava5cannotbeloadedbyJDK1.4orpreviousJVMs,usingJava5languagefeatureswouldlimittheircustomerbasetocompaniesthathavealreadytransitionedtoJava5.

AnothergroupofdevelopersoftenheldbackfromusingJava5arethoseworkingwithJavaEE.ManyshopswillnotuseJava5withJavaEE1.4orearlierforfearthatitwillnotbesupportedbytheirapplicationservervendor.ButitmaybeawhilebeforethoseshopstransitiontoJavaEE5.InadditiontothelagtimebetweentheJavaEE5andJavaSE5specifications,commercialJavaEE5containersarenotnecessarilyavailableassoonastheinkisdryonthespecification,businessesdonotnecessarilyupgradetheirapplicationsserversassoonasthenextversionisavailable,andevenafterupgradingtheirapplicationserver,itmaytakesometimetocertifytheirapplicationsonthenewplatform.

Java5languagefeatureimplementation

ThelanguagefeaturesaddedinJava5--generics,enumerations,annotations,autoboxing,andtheenhancedforloop--requirednochangetotheJVMinstructionset,andarealmostentirelyimplementedinthestaticcompiler(javac)andclasslibraries.Whenthecompilerencounterstheuseofgenerics,itattemptstoverifythattypesafetyispreserved(emittingan"

uncheckedcast"

warningifitcannot)andthenemitsbytecodethatisidenticaltowhatwouldbeproducedfromequivalentnongenericcode,castsandall.Similarly,autoboxingandtheenhancedforlooparesimply"

syntacticsugar"

forequivalent,butmoreverbose,idioms,andenumerationsarecompiledintoordinaryclasses.

Intheory,youcouldtaketheclassfilesproducedbyjavacandloadtheminearlierJVMs,whichwas,infact,theintentionwhenJSR14(theJavaCommunityProcessworkinggroupresponsibleforgenerics)wasconvened.However,otherissues(suchasretentionofannotations)forcedtheclassfileversiontobechangedbetweenJava1.4andJava5,whichpreventscodecompiledforJava5tobeloadedbyearlierJVMs.Further,someofthelanguagefeaturesaddedinJava5havedependenciesontheJava5libraries.Ifyoucompileaclasswithjavac-target1.5andtrytoloaditonanearlierJVM,you'

llgetanUnsupportedClassVersionErrorbecausethe-target1.5optiongeneratesclasseswithaclassfileversionof49,andJDK1.4onlysupportsclassfileversionsthrough48.

Thefor-eachloop

Theenhancedforloop,sometimescalledthefor-eachloop,istranslatedbythecompilerasiftheprogrammerhadsuppliedtheequivalentold-styleforloop.Thefor-eachloopcaniterateovertheelementsofanarrayorofacollection.Listing1showsthesyntaxofiteratingoveracollectionwiththefor-eachloop:

Listing1.Thefor-eachloop

Collection<

Foo>

fooCollection=...

for(Foof:

fooCollection){

doSomething(f);

}

Thecompilertranslatesthiscodeintotheequivalentiterator-basedloop,asshowninListing2:

Listing2.Iterator-basedequivalentforListing1

for(Iterator<

iter=f.iterator();

f.hasNext();

){

Foof=(Foo)iter.next();

Howdoesthecompilerknowthatthesuppliedargumenthasaniterator()method?

Thearchitectsofthejavaccompilercouldhavebuiltinunderstandingofthecollectionsframework,butthisapproachwouldhavebeenunnecessarilyrestrictive.Instead,anewinterfacewascreated,java.lang.Iterable(seeListing3),andthecollectionclasseswereretrofittedtoimplementIterable.Thisway,containerclassesthatdonotbuildonthecorecollectionsframeworkcanstilltakeadvantageofthenewfor-eachloop.ButdoingsocreatesadependencyontheJava5classlibrarybecauseIterableisnotpresentintheJDK1.4library.

Listing3.TheIterableinterface

publicinterfaceIterable<

T>

{

Iterator<

iterator();

Enumerationsandautoboxing

Justlikethefor-eachloop,enumerationsrequiresupportfromtheclasslibrary.Whenthecompilerencountersanenumeratedtype,itgeneratesaclassthatextendsthelibraryclassjava.lang.Enum.But,justlikeIterable,theEnumclassisnotpresentintheJDK1.4classlibrary.

Similarly,autoboxingreliesonthevalueOf()methodsbeingaddedtotheprimitivewrapperclasses(suchasInteger).WhenboxingrequiresconversionfrominttoInteger,ratherthancallingnewInteger(int),thecompilergeneratesacalltoInteger.valueOf(int).TheimplementationofthevalueOf()methodsemploystheflyweightpatterntocachetheIntegerobjectsforcommonlyusedintegervalues(theJava6implementationcachesintegersfrom-128to127),whichmayimproveperformancebyeliminatingredundantinstantiations.And,justlikeIterableandEnum,thevalueOf()methodsarenotpresentintheJDK1.4classlibrary.

Varargs

Whenthecompilerencountersamethoddefinedwithavariable-lengthargumentlist,itconvertsitintoamethodthattakesanarrayoftheappropriatecomponenttype;

whenthecompilerencountersacalltoamethodwithavariable-lengthargumentlist,itboxestheargumentsintoanarray.

Annotations

Whenanannotationisdefined,itcanbeannotatedwith@Retention,whichdetermineswhatthecompilerwilldowithclasses,methods,orfieldsthatpossessthatannotation.ThedefinedretentionpoliciesareSOURCE(discardannotationdataatcompilation),CLASS(recordannotationsintheclassfile),orRUNTIME(recordannotationsintheclassfileandretainthematruntimesotheycanbeaccessedreflectively).

Otherlibrarydependencies

PriortoJava5,whenthecompilerencounteredanattempttoconcatenatetwostrings,itusedthehelperclassStringBuffertoperformtheconcatenation.InJava5andlater,itinsteadgeneratescallstothenewStringBuilderclass,whichisnotpresentintheJDK1.4andearlierclasslibraries.

AccessingJava5features

Becauseofdependenciesoflanguagefeaturesonlibrarysupport,eveniftheclassfilesproducedbytheJava5compilercouldbeloadedbyearlierJVMversions,executionwouldstillfailbecauseofclassloadingerrors.However,itshouldbepossibletosolvetheseproblemsbysuitablytransformingthebytecodebecausethesemissingclassesdonotcontainsubstantialnewfunctionality.

JSR14

DuringthedevelopmentoftheJavagenericsspecification(andotherlanguagefeaturesaddedinJava5),experimentalsupportwasaddedtothejavaccompilertoallowittoconsumeJava5languagefeaturesandgeneratebytecodethatcouldberunonaJava1.4JVM.Whilethesefeaturesarenotsupported(orevendocumented),theyareusedbyanumberofopensourceprojectstoallowdeveloperstocodeusingJava5languagefeaturesandproduceJARfilesthatcanbeusedonearlierJVMs.And,nowthatjavacisopensource,itispossiblethefeaturesmightbesupportedbyathirdparty.Toactivatethesefeatures,youcaninvokejavacwiththe-source1.5and-targetjsr14options.

TheJSR14targetmodeofjavaccausesthecompilertoemitJDK1.4-compatiblebytecodecorrespondingtoJava5languagefeatures:

∙Genericsandvarargs:

Thecastsinsertedbythecompilerinthepresenceofgenericshavenodependencyontheclasslibrary,andsotheycanexecuteequallywellonapre-5JVM.Similarly,thecodegeneratedbythecompilerinthepresenceofvariable-lengthargumentlistshasnodependencyontheclasslibrary.

∙for-eachloop:

Wheniteratingoveranarray,thecompilergeneratesaninductionvariableandthestandardarrayiterationidiom.WheniteratingoveraCollection,thecompilergeneratesthestandarditerator-basedidiom.Wheniteratingoveranon-CollectionIterable,thecompilerproducesanerror.

∙Autoboxing:

RatherthangeneratingcallstothevalueOf()methodinthewrapperclass,thecompilergeneratescallstotheconstructorinstead.

∙Stringconcatenation:

TheJSR14targetmodeofjavaccausesthecompilertogeneratecallstoStringBufferinsteadofStringBuilder.

∙Enumerations:

TheJSR14targetmodeofjavachasnospecialsupportforenumerations.CodethatattemptstouseenumerationswillfailwithaNoClassDefFoundErrorlookingforthejava.lang.Enumbaseclass.

UsingtheJSR14targetmodeallowsyoutowritecodethatusesgenerics,autoboxing,andthefor-eachloopinthe"

easy"

cases,whichmaysufficeformanyprojects.Itisconvenient,ifunsupported,andthecompilergeneratesmostlycompatiblebytecodeinasinglepass.

Retroweaver

TherearecertainJava5languagefeaturesnotsupportedbytheJSR14targetmode(suchasIterableandenumerations).Analternateapproach,takenbyopen-sourceprojectssuchasRetroweaverandRetrotranslator,istogeneratebytecodeusing-ta

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 党团工作 > 入党转正申请

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2