王中王三肖选一肖

    HUANYINGFANGWENBEIDAQINGNIAOLUGUANGXIAOQU~

  1. 加入收藏
  2. 设为主页
  3. 学校简介

三分钟了解北大青鸟

×

Java编程中“为了性能”尽量要做的几点

责任编辑:027hpedu来源:北大青鸟鲁广校区Java技术教程发布时间:2012-03-21 15:02
导读:武汉北大青鸟鲁广校区: Java编程中为了性能尽量要做的几点 近的机器内存又爆满了,除了新增机器内存外,还应该好好review一下我们的代码,有很多代码编写过于随意化,这些不好

  武汉北大青鸟鲁广校区:Java编程中“为了性能”尽量要做的几点
    近的机器内存又爆满了,除了新增机器内存外,还应该好好review一下我们的代码,有很多代码编写过于随意化,这些不好的习惯或对程序语言的不了解是应该好好打压打压了。

  XIAMIANSHICANKAOWANGLUOZIYUANZONGJIEDEYIXIEZAIJavaBIANCHENGZHONGJINKENENGYAOZUODAODEYIXIEDIFANG。

王中王三肖选一肖   1. JINLIANGZAIHESHIDECHANGHESHIYONGDANLI

王中王三肖选一肖   SHIYONGDANLIKEYIJIANQINGJIAZAIDEFUDAN,SUODUANJIAZAIDESHIJIAN,TIGAOJIAZAIDEXIAOLV,DANBINGBUSHISUOYOUDIFANGDOUSHIYONGYUDANLI,JIANDANLAISHUO,DANLIZHUYAOSHIYONGYUYIXIASANGEFANGMIAN:

  ,KONGZHIZIYUANDESHIYONG,TONGGUOXIANCHENGTONGBULAIKONGZHIZIYUANDEBINGFAFANGWEN;

  DIER,KONGZHISHILIDECHANSHENG,YIDADAOJIEYUEZIYUANDEMUDE;

  DISAN,KONGZHISHUJUGONGXIANG,ZAIBUJIANLIZHIJIEGUANLIANDETIAOJIANXIA,RANGDUOGEBUXIANGGUANDEJINCHENGHUOXIANCHENGZHIJIANSHIXIANTONGXIN。

  2. JINLIANGBIMIANSUIYISHIYONGJINGTAIBIANLIANG

  YAOZHIDAO,DANGMOUGEDUIXIANGBEIDINGYIWEIstataicBIANLIANGSUOYINYONG,NEIMEgcTONGCHANGSHIBUHUIHUISHOUZHEIGEDUIXIANGSUOZHANYOUDENEICUN,RU

  public class A{ static B b = new B(); }

  CISHIJINGTAIBIANLIANGbDESHENGMINGZHOUQIYUALEITONGBU,RUGUOALEIBUHUIXIEZAI,NEIMEbDUIXIANGHUICHANGZHUNEICUN,ZHIDAOCHENGXUZHONGZHI。

  3. JINLIANGBIMIANGUODUOGUOCHANGDECHUANGJIANJavaDUIXIANG

王中王三肖选一肖   JINLIANGBIMIANZAIJINGCHANGDIAOYONGDEFANGFA,XUNHUANZHONGnewDUIXIANG,YOUYUXITONGBUYAOHUAFEISHIJIANLAICHUANGJIANDUIXIANG,ERQIEHAIYAOHUASHIJIANDUIZHEIXIEDUIXIANGJINXINGLAJIHUISHOUHECHULI,ZAIWOMENKEYIKONGZHIDEFANWEINEI,DAXIANDUDEZHONGYONGDUIXIANG,NENGYONGJIBENDESHUJULEIXINGHUOSHUZULAITIDAIDUIXIANG。

王中王三肖选一肖   4. JINLIANGSHIYONGfinalXIUSHIFU

  DAIYOUfinalXIUSHIFUDELEISHIBUKEPAISHENGDE。ZAIJavaHEXINAPIZHONG,YOUXUDUOYINGYONGfinalDELIZI,LIRUjava.lang.String。WEIStringLEIZHIDINGfinalFANGZHILESHIYONGZHEFUGAIlength()FANGFA。LINGWAI,RUGUOYIGELEISHIfinalDE,ZEGAILEISUOYOUFANGFADOUSHIfinalDE。JavaBIANYIQIHUIXUNZHAOJIHUINEILIAN(inline)SUOYOUDEfinalFANGFA(ZHEIHEJUTIDEBIANYIQISHIXIANYOUGUAN)。CIJUNENGGOUSHIXINGNENGPINGJUNTIGAO50%。

  5. JINLIANGSHIYONGJUBUBIANLIANG

  DIAOYONGFANGFASHICHUANDIDECANSHUYIJIZAIDIAOYONGZHONGCHUANGJIANDELINSHIBIANLIANGDOUBAOCUNZAIZHAN(Stack)ZHONG,SUDUJIAOKUAI。QITABIANLIANG,RUJINGTAIBIANLIANG、SHILIBIANLIANGDENG,DOUZAIDUI(Heap)ZHONGCHUANGJIAN,SUDUJIAOMAN。

  6. JINLIANGCHULIHAOBAOZHUANGLEIXINGHEJIBENLEIXINGLIANGZHEDESHIYONGCHANGSUO

  SUIRANBAOZHUANGLEIXINGHEJIBENLEIXINGZAISHIYONGGUOCHENGZHONGSHIKEYIXIANGHUZHUANHUAN,DANTAMENLIANGZHESUOCHANSHENGDENEICUNQUYUSHIWANBUTONGDE,JIBENLEIXINGSHUJUCHANSHENGHECHULIDOUZAIZHANZHONGCHULI,BAOZHUANGLEIXINGSHIDUIXIANG,SHIZAIDUIZHONGCHANSHENGSHILI。

  ZAIJIHELEIDUIXIANG,YOUDUIXIANGFANGMIANXUYAODECHULISHIYONGBAOZHUANGLEIXING,QITADECHULITICHANGSHIYONGJIBENLEIXING。

王中王三肖选一肖   7. SHENYONGsynchronized,JINLIANGJIANXIAOsynchronizeDEFANGFA

王中王三肖选一肖   DOUZHIDAO,SHIXIANTONGBUSHIYAOHENDADEXITONGKAIXIAOZUOWEIDAIJIADE,SHENZHIKENENGZAOCHENGSISUO,SUOYIJINLIANGBIMIANWUWEIDETONGBUKONGZHI。synchronizeFANGFABEIDIAOYONGSHI,ZHIJIEHUIBADANGQIANDUIXIANGSUO LE,ZAIFANGFAZHIXINGWANZHIQIANQITAXIANCHENGWUFADIAOYONGDANGQIANDUIXIANGDEQITAFANGFA。SUOYIsynchronizeDEFANGFAJINLIANGXIAO,BINGQIEYINGJINLIANGSHIYONGFANGFATONGBUDAITIDAIMAKUAITONGBU。

王中王三肖选一肖   8. JINLIANGSHIYONGStringBuilderHEStringBufferJINXINGZIFUCHUANLIANJIE

  ZHEIGEJIUBUDUOJIANGLE。

  9. JINLIANGBUYAOSHIYONGfinalizeFANGFA

  SHIJISHANG,JIANGZIYUANQINGLIFANGZAIfinalizeFANGFAZHONGWANCHENGSHIFEICHANGBUHAODEXUANZE,YOUYUGCDEGONGZUOLIANGHENDA,YOUQISHIHUISHOUYoungDAINEICUNSHI,DADOUHUIYINQIYINGYONGCHENGXUZANTING,SUOYIZAIXUANZESHIYONGfinalizeFANGFAJINXINGZIYUANQINGLI,HUIDAOZHIGCFUDANGENGDA,CHENGXUYUNXINGXIAOLVGENGCHA。

王中王三肖选一肖   10. JINLIANGSHIYONGJIBENSHUJULEIXINGDAITIDUIXIANG

王中王三肖选一肖   String str = "hello";

  SHANGMIANZHEIZHONGFANGSHIHUICHUANGJIANYIGE“hello”ZIFUCHUAN,ERQIEJVMDEZIFUHUANCUNCHIHAIHUIHUANCUNZHEIGEZIFUCHUAN;

  String str = new String("hello");

  CISHICHENGXUCHUCHUANGJIANZIFUCHUANWAI,strSUOYINYONGDEStringDUIXIANGDICENGHAIBAOHANYIGEchar[]SHUZU,ZHEIGEchar[]SHUZUYICICUNFANGLEh,e,l,l,o

  11. DANXIANCHENGYINGJINLIANGSHIYONGHashMap、ArrayList

王中王三肖选一肖   HashTable、VectorDENGSHIYONGLETONGBUJIZHI,JIANGDILEXINGNENG。

王中王三肖选一肖   12. JINLIANGHELIDECHUANGJIANHashMap

  DANGNIYAOCHUANGJIANYIGEBIJIAODADEhashMapSHI,CHONGFENLIYONGLINGYIGEGOUZAOHANSHU

  public HashMap(int initialCapacity, float loadFactor)

  BIMIANHashMapDUOCIJINXINGLEhashZHONGGOU,KUORONGSHIYIJIANHENHAOFEIXINGNENGDESHI,ZAIMORENZHONGinitialCapacityZHIYOU16,ERloadFactorSHI 0.75,XUYAODUODADERONGLIANG,NINENGZHUNQUEDEGUJINISUOXUYAODELIXIANGDAXIAO,TONGYANGDEHashtable,VectorsYESHIYIYANGDEDAOLI。

王中王三肖选一肖   13. JINLIANGJIANSHAODUIBIANLIANGDEZHONGFUJISUAN

  RU

  for(int i=0;i

  YINGGAIGAIWEI

  for(int i=0,len=list.size();i

  BINGQIEZAIXUNHUANZHONGYINGGAIBIMIANSHIYONGFUZADEBIAODASHI,ZAIXUNHUANZHONG,XUNHUANTIAOJIANHUIBEIFANFUJISUAN,RUGUOBUSHIYONGFUZABIAODASHI,ERSHIXUNHUANTIAOJIANZHIBUBIANDEHUA,CHENGXUJIANGHUIYUNXINGDEGENGKUAI。

王中王三肖选一肖   14. JINLIANGBIMIANBUBIYAODECHUANGJIAN

  RU

  A a = new A(); if(i==1){list.add(a);}

  YINGGAIGAIWEI

  if(i==1){ A a = new A(); list.add(a);}

  15. JINLIANGZAIfinallyKUAIZHONGSHIFANGZIYUAN

王中王三肖选一肖   CHENGXUZHONGSHIYONGDAODEZIYUANYINGDANGBEISHIFANG,YIBIMIANZIYUANXIELOU。ZHEIZAIfinallyKUAIZHONGQUZUO。BUGUANCHENGXUZHIXINGDEJIEGUORUHE,finallyKUAIZONGSHIHUIZHIXINGDE,YIQUEBAOZIYUANDEZHENGQUEGUANBI。

  16. JINLIANGSHIYONGYIWEILAIDAITI'a/b'DECAOZUO

  "/"SHIYIGEDAIJIAHENGAODECAOZUO,SHIYONGYIWEIDECAOZUOJIANGHUIGENGKUAIHEGENGYOUXIAO

  RU

王中王三肖选一肖   int num = a / 4; int num = a / 8;

  YINGGAIGAIWEI

  int num = a >> 2; int num = a >> 3;

王中王三肖选一肖   DANZHUYIDESHISHIYONGYIWEIYINGTIANJIAZHUSHI,YINWEIYIWEICAOZUOBUZHIGUAN,BIJIAONANLIJIE

王中王三肖选一肖   17.JINLIANGSHIYONGYIWEILAIDAITI'a*b'DECAOZUO

王中王三肖选一肖   TONGYANGDE,DUIYU'*'CAOZUO,SHIYONGYIWEIDECAOZUOJIANGHUIGENGKUAIHEGENGYOUXIAO

  RU

  int num = a * 4; int num = a * 8;

  YINGGAIGAIWEI

王中王三肖选一肖   int num = a << 2; int num = a << 3;

王中王三肖选一肖   18. JINLIANGQUEDINGStringBufferDERONGLIANG

王中王三肖选一肖   StringBuffer DEGOUZAOQIHUICHUANGJIANYIGEMORENDAXIAO(TONGCHANGSHI16)DEZIFUSHUZU。ZAISHIYONGZHONG,RUGUOCHAOCHUZHEIGEDAXIAO,JIUHUIZHONGXINFENPEINEICUN,CHUANGJIANYIGEGENGDADESHUZU,BINGJIANGYUANXIANDESHUZUFUZHIGUOLAI,ZAI DIUQIJIUDESHUZU。ZAIDADUOSHUQINGKUANGXIA,NIKEYIZAICHUANGJIAN StringBufferDESHIHOUZHIDINGDAXIAO,ZHEIYANGJIUBIMIANLEZAIRONGLIANGBUGOUDESHIHOUZIDONGZENGZHANG,YITIGAOXINGNENG。

  RU:

王中王三肖选一肖   StringBuffer buffer = new StringBuffer(1000);

  19. JINLIANGZAOSHIFANGWUYONGDUIXIANGDEYINYONG

  DABUFENSHI,FANGFAJUBUYINYONGBIANLIANGSUOYINYONGDEDUIXIANG HUISUIZHEFANGFAJIESHUERBIANCHENGLAJI,YINCI,DABUFENSHIHOUCHENGXUWUXUJIANGJUBU,YINYONGBIANLIANGXIANSHISHEWEInull。

  LIRU:

  Public void test(){ Object obj = new Object(); …… Obj=null; }

  SHANGMIANZHEIGEJIUMEIBIYAOLE,SUIZHEFANGFAtest()DEZHIXINGWANCHENG,CHENGXUZHONGobjYINYONGBIANLIANGDEZUOYONGYUJIUJIESHULE。DANSHIRUGUOSHIGAICHENGXIAMIAN:

  Public void test(){ Object obj = new Object(); …… Obj=null; //ZHIXINGHAOSHI,HAONEICUNCAOZUO;HUODIAOYONGHAOSHI,HAONEICUNDEFANGFA …… }

王中王三肖选一肖   ZHEISHIHOUJIUYOUBIYAOJIANGobjFUZHIWEInull,KEYIJINZAODESHIFANGDUIObjectDUIXIANGDEYINYONG。

  20. JINLIANGBIMIANSHIYONGERWEISHUZU

王中王三肖选一肖   ERWEISHUJUZHANYONGDENEICUNKONGJIANBIYIWEISHUZUDUODEDUO,DAGAI10BEIYISHANG。

王中王三肖选一肖   21. JINLIANGBIMIANSHIYONGsplit

王中王三肖选一肖   CHUFEISHIBIXUDE,FOUZEYINGGAIBIMIANSHIYONGsplit,splitYOUYUZHICHIZHENGZEBIAODASHI,SUOYIXIAOLVBIJIAODI,RUGUOSHIPINFANDEJISHI,JIBAIWANDEDIAOYONGJIANGHUIHAOFEIDALIANGZIYUAN,RUGUOQUESHIXU YAOPINFANDEDIAOYONGsplit,KEYIKAOLVSHIYONGapacheDEStringUtils.split(string,char),PINFANsplitDEKEYIHUANCUNJIEGUO。

  22. ArrayList & LinkedList

王中王三肖选一肖   YI GESHIXIANXINGBIAO,YIGESHILIANBIAO,YIJUHUA,SUIJICHAXUNJINLIANGSHIYONGArrayList,ArrayListYOUYULinkedList,LinkedListHAIYAOYIDONGZHI ZHEN,TIANJIASHANCHUDECAOZUOLinkedListYOUYUArrayList,ArrayListHAIYAOYIDONGSHUJU,BUGUOZHEISHILILUNXINGFENXI,SHISHIWEIBIRUCI,ZHONGYAODESHILIJIEHAO2 ZHEDESHUJUJIEGOU,DUIZHENGXIAYAO。

  23. JINLIANGSHIYONGSystem.arraycopy ()DAITITONGGUOLAIXUNHUANFUZHISHUZU

  System.arraycopy() YAOBITONGGUOXUNHUANLAIFUZHISHUZUKUAIDEDUO

  24. JINLIANGHUANCUNJINGCHANGSHIYONGDEDUIXIANG

王中王三肖选一肖   JINKENENGJIANGJINGCHANGSHIYONGDEDUIXIANGJINXINGHUANCUN,KEYISHIYONGSHUZU,HUOHashMapDERONGQILAIJINXINGHUANCUN,DANZHEIZHONGFANGSHIKENENGDAOZHIXITONGZHANYONGGUODUODEHUANCUN,XINGNENGXIAJIANG,KEYISHIYONGYIXIEDISANFANGDEKAIYUANGONGJU,RUEhCache,OscacheJINXINGHUANCUN,TAMENJIBENDOUSHIXIANLEFIFO/FLUDENGHUANCUNSUANFA。

  25. JINLIANGBIMIANFEICHANGDADENEICUNFENPEI

王中王三肖选一肖   YOUSHIHOUWENTIBUSHIYOUDANGSHIDEDUIZHUANGTAIZAOCHENGDE,ERSHIYINWEIFENPEISHIBAIZAOCHENGDE。FENPEIDENEICUNKUAIDOUBIXUSHILIANXUDE,ERSUIZHEDUIYUELAIYUEMAN,ZHAODAOJIAODADELIANXUKUAIYUELAIYUEKUNNAN。

王中王三肖选一肖   26. SHENYONGYICHANG

王中王三肖选一肖   DANGCHUANGJIANYIGEYICHANGSHI,XUYAOSHOUJIYIGEZHANGENZONG(stack track),ZHEIGEZHANGENZONGYONGYUMIAOSHUYICHANGSHIZAIHECHUCHUANGJIANDE。GOUJIANZHEIXIEZHANGENZONGSHIXUYAOWEIYUNXINGSHIZHANZUOYIFENKUAIZHAO,ZHENGSHIZHEIYIBUFENKAIXIAOHENDA。DANGXUYAOCHUANGJIANYIGE Exception SHI,JVM BUDEBUSHUO:XIANBIEDONG,WOXIANGJIUNINXIANZAIDEYANGZICUNYIFENKUAIZHAO,SUOYIZANSHITINGZHIRUZHANHECHUZHANCAOZUO。ZHANGENZONGBUZHIBAOHANYUNXINGSHIZHANZHONGDEYILIANGGEYUANSU,ERSHIBAOHANZHEIGEZHANZHONGDEMEIYIGEYUANSU。

  RU GUONINCHUANGJIANYIGE Exception ,JIUDEFUCHUDAIJIA。HAOZAIBUHUOYICHANGKAIXIAOBUDA,YINCIKEYISHIYONG try-catch JIANGHEXINNEIRONGBAOQILAI。CONGJISHUSHANGJIANG,NINSHENZHIKEYISUIYIDIPAOCHUYICHANG,ERBUYONGHUAFEIHENDADEDAIJIA。ZHAOZHIXINGNENGSUNSHIDEBINGBUSHI throw CAOZUO——JINGUANZAIMEIYOUYUXIANCHUANGJIANYICHANGDEQINGKUANGXIAJIUPAOCHUYICHANGSHIYOUDIANBUXUNCHANG。ZHENZHENGYAOHUADAIJIADESHICHUANGJIANYICHANG。XINGYUNDESHI,HAODEBIANCHENGXIGUANYIJIAOHUIWOMEN,BUYINGGAIBUGUANSANQIERSHIYIJIU PAOCHUYICHANG。YICHANGSHIWEIYICHANGDEQINGKUANGERSHEJIDE,SHIYONGSHIYEYINGGAILAOJIZHEIYIYUANZE。

本文标题:Java编程中“为了性能”尽量要做的几点,责任编辑:027hpedu,于2012年03月21日15时02分发布于北大青鸟鲁广校区。武汉北大青鸟宏鹏鲁广校区,是北大青鸟武汉电脑学校,专业IT计算机培训,拥有软件/网络/安卓/Web前端/Java课程.北大青鸟地址:光谷广场鲁巷数码港.电话:027-87807717

专业老师指导

赵老师

CONGSHIITJIAOYUPEIXUNSHINIANYOUYU,ZHILIYUBANGZHUGUANGDAXUEZIZHAODAOSHIHEZIJIDEZHUANYE。

培训咨询客服

刘老师

ITPEIXUNZHUANYEKEFU,YONGZIJIDEZHENCHENGJIEJUELEWUSHUXUEZIDEKUNHUO。

本文地址:
文章标题:
×
在线咨询更多问题

如您有任何疑问
在线咨询随时为您解答或拨打咨询热线:17740513250

×
  • 姓 名*
  • 手 机*
  • QQ号/微信号
  • 所报学科 *
  • 其他备注
  • 王中王三肖选一肖WENXINTISHI:QINGBAOCHISHOUJICHANGTONG,ZIXUNLAOSHIJIANGWEININTIGONGZHUANSHUDEYIDUIYIBAOMINGFUWU。