(1) bigdecimal中要使用string作為參數(shù)來構(gòu)造(bigdecimal.valueof()會把各種值轉(zhuǎn)化成字符串,再調(diào)用 bigdecimal(string)的) ,否則照樣會出問題;
- system.out.println( 10000 * 1.005 );????????system.out.println( new ?bigdecimal( 10000 ).multiply( new ?bigdecimal( 1.005 )));??????system.out.println(bigdecimal.valueof( 10000 ).multiply(bigdecimal.valueof( 1.005 )));??????system.out.println( new ?bigdecimal( "10000" ).multiply( new ?bigdecimal( "1.005" )));??
system.out.println(10000*1.005); system.out.println(new bigdecimal(10000).multiply(new bigdecimal(1.005))); system.out.println(bigdecimal.valueof(10000).multiply(bigdecimal.valueof(1.005))); system.out.println(new bigdecimal("10000").multiply(new bigdecimal("1.005")));
輸出:
10049.999999999998
10049.99999999999893418589635984972119331359863281250000
10050.000
10050.000
(2)
長整型要使用大寫字母l而不是小寫字母l來標(biāo)識
,因為小寫字母l看起來有點像數(shù)字1;
例如:
- long ?i?=?200l; //看起來是不是像2001呢;????long?i?=?200l;//這樣,就算有近視,也能辨認(rèn)清楚了。 ??
long i = 200l;//看起來是不是像2001呢; long i = 200l;//這樣,就算有近視,也能辨認(rèn)清楚了。
(3)
如果一個類中有多處使用長數(shù)字,則可以定義并使用數(shù)字常量
,到處寫長的數(shù)字,容易出現(xiàn)多一個0或少一0等寫錯位數(shù)的問題,同時導(dǎo)致問題難以排查。
例如:
- if (i?>? 10000000 ?&&?i< 100000000 )?{...} if (j?>=? 10000000 ?&&?j<= 100000000 )?{...}??
if(i > 10000000 && i<100000000) {...}if(j >= 10000000 && j<=100000000) {...}
可改成:
- final ? int ?begin?=? 10000000 ; //或?final?int?begin?=?10*1000*1000;final?int?end?=?100000000;//或?final?int?end?=?100*1000*1000if?(i?>?begin?&&?i<?end){...}if?(j?>=?begin?&&?j<=?end){...} ??
final int begin = 10000000;//或 final int begin = 10*1000*1000;final int end = 100000000;//或 final int end = 100*1000*1000if (i > begin && i< end){...}if (j >= begin && j<= end){...}
再如timeunit中的代碼:
- //?handy?constants?for?conversion?methods????static?final?long?c0?=?1l;????static?final?long?c1?=?c0?*?1000l;????static?final?long?c2?=?c1?*?1000l;????static?final?long?c3?=?c2?*?1000l;????static?final?long?c4?=?c3?*?60l;????static?final?long?c5?=?c4?*?60l;????static?final?long?c6?=?c5?*?24l; ??
// handy constants for conversion methods static final long c0 = 1l; static final long c1 = c0 * 1000l; static final long c2 = c1 * 1000l; static final long c3 = c2 * 1000l; static final long c4 = c3 * 60l; static final long c5 = c4 * 60l; static final long c6 = c5 * 24l;
(4)
在使用map時,要注意integer與long的自動裝箱,要與string類型的key區(qū)分開來
:
例如:
- map<string,string>?hm?=? new ?hashmap<string,string>();???????hm.put( "1" ,? "1value" );??????hm.put( "2" ,? "1value" );??????hm.put( "3" ,? "1value" );??????hm.put( "4" ,? "1value" );?????????????? int ?i?=? 1 ;??????system.out.println(hm.get(i+ 1 )); //錯誤的做法,值為null??????system.out.println(hm.get(string.valueof(i+1)));//正確的做法 ??
map<string,string> hm = new hashmap<string,string>(); hm.put("1", "1value"); hm.put("2", "1value"); hm.put("3", "1value"); hm.put("4", "1value"); int i = 1; system.out.println(hm.get(i+1));//錯誤的做法,值為null system.out.println(hm.get(string.valueof(i+1)));//正確的做法
輸出:
null
1value
因為i+1計算后自動裝箱成integer(2),而hm.get(new integer(2)) 與hm.get(new string(2))是完全不一樣的。
(5)有同學(xué)寫代碼,把
數(shù)字轉(zhuǎn)型為字串
時喜歡用類似 i+"" 這種方式,個人認(rèn)為這樣的代碼比較難看,可以寫成
string.valueof(i),
它是不是好看一些。
(6)
對于復(fù)雜的判斷條件,最好不要經(jīng)常使用“非”等排除條件式子
,這樣不便于理解組合條件的含義:
例如:
?? 如果 成功方案贏利≥1000元 或 成功方案贏利≥500元且回報率≥10倍 則返回戰(zhàn)績,否則 返回0;
不便于直接思維的、排除式的寫法:
- if ?(profit.compareto(minprofit)?<? 0 ?&&?(profit.compareto(minrateprofit)?<? 0 ?||?rate.compareto(minrate)?<? 0 ))?{???????????? return ? 0 ;?}? else ?{ return ?戰(zhàn)績值;}??
if (profit.compareto(minprofit) < 0 && (profit.compareto(minrateprofit) < 0 || rate.compareto(minrate) < 0)) { return 0; } else {return 戰(zhàn)績值;}
這個是正確的寫法,但是因為整合條件都要反過來寫,類似:a&&b 變成 !a||!b,很難理解,也極容易把(!a || !b)寫成 (!a && !b)。
便于理解的、直觀式的寫法:
- if ?(profit.compareto(minprofit)?>=? 0 ?&&?(profit.compareto(minrateprofit)?>=? 0 ?&&?rate.compareto(minrate)?>=? 0 ))?{??????? return ?戰(zhàn)績值;} else ?{??? return ? 0 ;?}??
if (profit.compareto(minprofit) >= 0 && (profit.compareto(minrateprofit) >= 0 && rate.compareto(minrate) >= 0)) { return 戰(zhàn)績值;}else { return 0; }
(7)經(jīng)常看見list!=null && list.size()>0 這樣的代碼,大概擔(dān)心用到的list為null而導(dǎo)致空指針異常,其實list!=null一般是沒有必要,這樣的代碼比較難看一點。
《effective java》建議:對返回值為collection或map的子類的,盡量返回一個元素為空的collection或map,而不是返回null。
事實上,很多開源的框架中就是這么做的,看下面應(yīng)用hibernate的一段代碼:
- query?q?=?*dao.createquery(hql);???????list<?>?list?=?q.list();??????? if ?(list?!=? null ?&&?list.size()?>? 0 )?{?????????? return ?(dcrace)?list.get( 0 );???????}??
query q = *dao.createquery(hql); list<?> list = q.list(); if (list != null && list.size() > 0) { return (dcrace) list.get(0); }
可以改為:
- query?q?=?*dao.createquery(hql);???????list<?>?list?=?q.list();??????? if ?(list.size()?>? 0 )?{ //刪除?list?!=?null?&&???????????return?(dcrace)?list.get(0);???????} ??
query q = *dao.createquery(hql); list<?> list = q.list(); if (list.size() > 0) {//刪除 list != null && return (dcrace) list.get(0); }
事實上,q.list()永遠(yuǎn)不會返回null,所以沒有必要判斷l(xiāng)ist是否為null,這樣代碼更加簡潔、好看。
(8)bigdecimal是一個只讀類,它的對象一旦構(gòu)建好了,就無法修改該該對象的屬性。如:
public bigdecimal setscale(int newscale, int roundingmode)
public bigdecimal add(bigdecimal augend)
等方法都是返回一個新的bigdecimal對象,而對原來的bigdecimal不會起熱任何修改作用的。jdk中類似的只讀類還有:所有的基本數(shù)據(jù)類型,所有的數(shù)據(jù)封裝類,biginteger,string等。
?
轉(zhuǎn)自: http://haouziwefe.iteye.com/blog/1147281
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
