Topic: coding 注意事项 |
Print this page |
1.coding 注意事项 | Copy to clipboard |
Posted by: ysheng Posted on: 2005-12-09 11:10 http://peterbull.mblogger.cn/ 在进行常量与变量比较时,建议把常量放在前面(如 if (null==name){}) eg:!name.equals("") 如果name为空的时候会造成NullPointerException 建议采用!"".equals(name) |
2.Re:coding 注意事项 [Re: ysheng] | Copy to clipboard |
Posted by: liuliu1231111 Posted on: 2005-12-09 12:32 字符串有的是.length()方法, 而数组是.length属性。 这个算么? 呵呵。 |
3.Re:coding 注意事项 [Re: ysheng] | Copy to clipboard |
Posted by: liuliu1231111 Posted on: 2005-12-09 12:38 switch(expr) expr可以是一个整数表达式,case 的参数可以是int,short,char或byte 不可以是long,string. |
4.Re:coding 注意事项 [Re: ysheng] | Copy to clipboard |
Posted by: ysheng Posted on: 2005-12-09 16:42 使用 StringBuffer 也需要注意效率 很多时候我们习惯这样用 sbf.append("a line"+"\r\n"); 但下面这样用要减少一半的时间,虽然代码增加了1行 sbf.append("a line"); sbf.append("\r\n"); |
5.Re:coding 注意事项 [Re: ysheng] | Copy to clipboard |
Posted by: why Posted on: 2005-12-09 19:02 ysheng wrote: sbf.append("a line").append("\r\n"); actually sbf.append("a line\r\n"); // Anyway I guess that ysheng was thinking about a variable instead of just a literal "a line" or better be sbf.append("a line").append(System.getProperty("line.separator")); // don't hardcode the line separator for portability // of course, one would store System.getProperty("line.separator") somewhere first |
6.Re:coding 注意事项 [Re: why] | Copy to clipboard |
Posted by: ysheng Posted on: 2005-12-09 23:21 why wrote: |
7.Re:coding 注意事项 [Re: ysheng] | Copy to clipboard |
Posted by: ftang Posted on: 2005-12-09 23:37 ysheng wrote: you didn't get the point here: "a" + "b" + "c" is bad expierence because in memory JVM will create "a", "b", "ab","c", "abc" which here, "ab" is a waste...so you should always use StringBuffer. Get the point...know why...ALL the tips here are Computer Sciense 101, but you guys should know why....WHY!! not only know HOW |
8.Re:coding 注意事项 [Re: ysheng] | Copy to clipboard |
Posted by: zzw109433 Posted on: 2005-12-14 10:55 String str=null; for(int i=0;i<10;i++){ str=new String(); } 不要写成 for(int i=0;i<10;i++){ String str=new String(); } |
9.Re:coding 注意事项 [Re: zzw109433] | Copy to clipboard |
Posted by: zcjl Posted on: 2005-12-14 12:03 zzw109433 wrote: 这个似乎搞反了吧? 倒是后一种方法是推荐的,因为每次循环生成的对象的可达域都在循环体内,循环结束即可被gc回收 而前一种写法,最后一次循环生成的对象被循环体外的reference引用着,无法即时被回收 |
10.Re:coding 注意事项 [Re: zcjl] | Copy to clipboard |
Posted by: scarecrow Posted on: 2005-12-15 13:00 zcjl wrote: >> 为每次循环生成的对象的可达域都在循环体内,循环结束即可被gc回收 你不认为这样会增加gc 的负担么? 另外每次都要再循环体内创建一个新的String引用 str 也增加了系统的开销。 |
11.Re:coding 注意事项 [Re: ysheng] | Copy to clipboard |
Posted by: javadd Posted on: 2005-12-15 14:00 ysheng wrote: |
12.Re:coding 注意事项 [Re: scarecrow] | Copy to clipboard |
Posted by: zcjl Posted on: 2005-12-15 15:53 scarecrow wrote: 首先,这不会增加gc的负担 其次,这样做,是基于《Effective Java》里面的建议:第29条:将局部变量的作用域最小化 |
13.Re:coding 注意事项 [Re: ftang] | Copy to clipboard |
Posted by: ranchgirl Posted on: 2005-12-15 17:23 ftang wrote: What you said is not necessary true, actually, not true. From SLS at http://java.sun.com/docs/books/jls/ |
14.Re:coding 注意事项 [Re: zcjl] | Copy to clipboard |
Posted by: ranchgirl Posted on: 2005-12-15 17:43 zcjl wrote: You are correct, I wrote two methods, compiled, and disaasemblied the byte code, and proved you are correct. |
15.Re:coding 注意事项 [Re: zcjl] | Copy to clipboard |
Posted by: scarecrow Posted on: 2005-12-15 18:03 zcjl wrote: >>首先,这不会增加gc的负担 能给出你得出这个结论的理由么? >>其次,这样做,是基于《Effective Java》里面的建议:第29条:将局部变量的作用域最小化 将局部变量的作用域最小化,没错。不过这应该对于if ,try switch等语句说的吧,对与循环我认为还是不妥当的,特别是当循环的次数比较大的时候。 |
16.Re:coding 注意事项 [Re: gongshi] | Copy to clipboard |
Posted by: ftang Posted on: 2005-12-16 03:50 gongshi wrote: true or not I think you should point out from when...JVM is improving every day... this kind of stupid problem Sun won't miss. This theory is not from my idea it is from Joshua Bloch's Effective Java Programming Language. I forgot the which page...but you can easily find it out... plus...I bring this up here is because I want the beginner know when they learn the tricks they need to know why... |
17.Re:coding 注意事项 [Re: ftang] | Copy to clipboard |
Posted by: ranchgirl Posted on: 2005-12-16 03:54 ftang wrote: No, I was not accusing you at all. If I go to an interview, I would answer exact the same as you pointed out. However, I would probably add a note after the interviewer was satisfied, to get a "plus sign"... Haha |
18.Re:coding 注意事项 [Re: ysheng] | Copy to clipboard |
Posted by: ftang Posted on: 2005-12-16 03:59 haha....use this question as interview question is too easy...man...at your level I think you would ask some questions much harder than this one... |
19.Re:coding 注意事项 [Re: scarecrow] | Copy to clipboard |
Posted by: scarecrow Posted on: 2005-12-16 10:04 scarecrow wrote: 自己做了一次测试在1000000000次的循环下分别使用两种代码书写方式耗费时间基本上是没有差别的。实际的结果和自己想象的不太一样,事实证明的zcjl 的观点应该是正确的。不过那位能给出更确切的理论依据。 |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |