Java-7的64个新特性

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

Copyright©2012,Oracleand/oritsaffiliates.Allrightsreserved.InsertInformationProtectionPolicyClassificationfromSlide131Copyright©2012,Oracleand/oritsaffiliates.Allrightsreserved.InsertInformationProtectionPolicyClassificationfromSlide13255NewThingsinJava7InsertPresenter’sNameHereInsertPresenter’sTitleHereCopyright©2012,Oracleand/oritsaffiliates.Allrightsreserved.InsertInformationProtectionPolicyClassificationfromSlide13355NewThingsinJava7InsertPresenter’sNameHereInsertPresenter’sTitleHerePresentingwithTHIRDPARTYCOMPANYLOGOORACLEPRODUCTLOGOCopyright©2012,Oracleand/oritsaffiliates.Allrightsreserved.InsertInformationProtectionPolicyClassificationfromSlide134The64NewJava7Features•64minutes–64slides•IgniteFormat•Nostopping!•Nogoingback!•ThePRDforJava7hadalmost2,000changes,hereare64ofthemorerelevantones...(thatyouprobablydidn’thearabout)Copyright©2012,Oracleand/oritsaffiliates.Allrightsreserved.InsertInformationProtectionPolicyClassificationfromSlide135BinaryLiteralsintmask=0b101010101010;aShort=(short)0b1010000101000101;longaLong=0b1010000101000101101000010100010110100001010001011010000101000101L;HAPPY_FACE={(short)0b0000011111100000;(short)0b0000100000010000;(short)0b0001000000001000;(short)0b0010000000000100;(short)0b0100000000000010;(short)0b1000011001100001;(short)0b1000011001100001;(short)0b1000000000000001;(short)0b1000000000000001;(short)0b1001000000001001;(short)0b1000100000010001;(short)0b0100011111100010;(short)0b0010000000000100;(short)0b0001000000001000;(short)0b0000100000010000;(short)0b0000011111100000;}Copyright©2012,Oracleand/oritsaffiliates.Allrightsreserved.InsertInformationProtectionPolicyClassificationfromSlide136UnderscoresinNumberLiterals•Valid:•Invalid:intmask=0b1010_1010_1010;longbig=9_223_783_036_967_937L;longcreditCardNumber=1234_5678_9012_3456L;longsocialSecurityNumber=999_99_9999L;floatpi=3.14_15F;longhexBytes=0xFF_EC_DE_5E;longhexWords=0xCAFE_BFFE;floatpi1=3_.1415F;floatpi2=3._1415F;longssn=999_99_9999_L;intx1=_52;intx1=52_;intx2=0_x52;intx2=0x_52;Copyright©2012,Oracleand/oritsaffiliates.Allrightsreserved.InsertInformationProtectionPolicyClassificationfromSlide137StringsinSwitchStatementsintmonthNameToDays(Strings,intyear){switch(s){caseApril:caseJune:caseSeptember:caseNovember:return30;caseJanuary:caseMarch:caseMay:caseJuly:caseAugust:caseDecember:return31;caseFebruary”:...default:...Didyouknowitproducesgenerallymoreefficientbytecodesthananif-then-elsestatement?CaseSensitive!Copyright©2012,Oracleand/oritsaffiliates.Allrightsreserved.InsertInformationProtectionPolicyClassificationfromSlide138AutomaticResourceManagementtry(InputStreamin=newFileInputStream(src),OutputStreamout=newFileOutputStream(dest)){byte[]buf=newbyte[8192];intn;while(n=in.read(buf))=0)out.write(buf,0,n);}•Newsuperinterfacejava.lang.AutoCloseable•AllAutoCloseable(throwsException)andbyextensionjava.io.Closeable(throwsIOException)typesuseablewithtry-with-resources•Anythingwithavoidclose()methodisacandidate•JDBC4.1retrofittedasAutoCloseabletooCopyright©2012,Oracleand/oritsaffiliates.Allrightsreserved.InsertInformationProtectionPolicyClassificationfromSlide139SurpressedExceptionsjava.io.IOExceptionatSuppress.write(Suppress.java:19)atSuppress.main(Suppress.java:8)Suppressed:java.io.IOExceptionatSuppress.close(Suppress.java:24)atSuppress.main(Suppress.java:9)Suppressed:java.io.IOExceptionatSuppress.close(Suppress.java:24)atSuppress.main(Suppress.java:9)Throwable.getSupressed();//ReturnsThrowable[]Throwable.addSupressed(aThrowable);Copyright©2012,Oracleand/oritsaffiliates.Allrightsreserved.InsertInformationProtectionPolicyClassificationfromSlide1310Multi-Catchtry{...}catch(ClassCastExceptione){doSomethingClever(e);throwe;}catch(InstantiationException|NoSuchMethodException|InvocationTargetExceptione){//Usefulifyoudogenericactionslog(e);throwe;}Copyright©2012,Oracleand/oritsaffiliates.Allrightsreserved.InsertInformationProtectionPolicyClassificationfromSlide1311MorePreciseRethrowpublicvoidfoo(Stringbar)throwsFirstException,SecondException{try{//Codethatmaythrowboth//FirstExceptionandSecondException}catch(Exceptione){throwe;}}•PriortoJava7,thiscodewouldnotcompile,thetypesinthrowswouldhavetomatchthetypesincatch–foowouldhaveto“throwsException”•Java7addssupportforthisaslongastryblockcallsalltheexceptionsinthethrowsclause,thatthevariableinthecatchclauseisthevariablethatisrethrownandtheexceptionsarenotcaughtbyanothercatchblock.Copyright©2012,Oracleand/oritsaffiliates.Allrightsreserved.InsertInformationProtectionPolicyClassificationfromSlide1312DiamondOperatorWorksManyWays...•Withdiamond()compilerinferstypeListStringstrList=newArrayList();ORListMapString,ListStringstrList=newArrayList();ORFooBarfoo=newFoo();foo.mergeFoo(newFoo());Copyright©2012,Oracleand/oritsaffiliates.Allrightsreserved.InsertInformationProtectionPolicyClassificationfromSlide1313VarangsWarnings-ErasureclassTest{publicstaticvoidmain(String...args){ListListStringmonthsInTwoLanguages=Arrays.asList(Arrays.asList(January,February),Arrays.asList(G

1 / 74
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功