您好,欢迎访问三七文档
当前位置:首页 > IT计算机/网络 > 数据库 > 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
本文标题:Java-7的64个新特性
链接地址:https://www.777doc.com/doc-3376706 .html