qindyyx 发表于 2013-2-3 10:16:45

Java编程40条

1.使用构造函数 string() 带来的内存性能隐患和缓解
用户自己构建缓存,这种方式的优点是更加灵活。创建HashMap,将需缓存的String作为key和value存放入HashMap。假设我们准备创建的字符串为 key,将Map cacheMap作为缓冲池,那么返回 key 的代码如下:
  private String getCacheWord(String key) { 
     String tmp = cacheMap.get(key); 
     if(tmp != null) { 
            return tmp; 
     } else { 
             cacheMap.put(key, key); 
             return key; 
     } 
  } 
 
 
 
2.在拼接动态字符串时,尽量用StringBuffer或StringBuilder的append,这样可以减少构造过多的临时String对象。
如果知道StringBuilder的大小的话,最好初始化容量
 
StringBuffer sb = new StringBuffer("Hello" + getWorld());
sb.append("Calling From " + getJava());
正确的做法:
StringBuffer sb = new StringBuffer("Hello");
sb.append(getWorld());
sb.append("Calling From ");
sb.append(getJava());
 
 
 
3.如果只是查找单个字符的话,用charat()代替startswith() .
 
 
 
4.在字符串相加的时候,使用 ' ' 代替 " ",如果该字符串只有一个字符的话
 
 
 
5.不要在循环中调用synchronized(同步)方法 
 
 
 
6.整合try/catch.将try/catch块移出循环.也尽量不要嵌套
 
 
 
7.用stringtokenizer代替indexof()和substring()
 
 
 
8.使用'system.arraycopy ()'代替通过来循环复制数组
 
 
 
9.不用new关键字创建对象的实例
 
用new关键词创建类的实例时,构造函数链中的所有构造函数都会被自动调用。但如果一个对象实现了Cloneable接口,我们可以调用她的clone()方法。clone()方法不会调用任何类构造函数。
下面是Factory模式的一个典型实现。
public static Credit getNewCredit()
{
    return new Credit();
}
改进后的代码使用clone()方法,
private static Credit BaseCredit = new Credit();
public static Credit getNewCredit()
{
    return (Credit)BaseCredit.clone();
}
 
 
 
10.HaspMap的遍历
 
Map<String, String[]> paraMap = new HashMap<String, String[]>();
for( Entry<String, String[]> entry : paraMap.entrySet() )
{
    String appFieldDefId = entry.getKey();
    String[] values = entry.getValue();
}
利用散列值取出相应的Entry做比较得到结果,取得entry的值之后直接取key和value
 
 
 
11.尽量指定类为final
如果指定一个类为final,则该类所有的方法都是final。Java编译器会寻找机会内联(inline)所有的final方法(这和具体的编译器实现有关)。此举能够使性能平均提高50%.
 
 
 
12.尽量使用局部变量-调用中创建的临时变量都保存在栈(Stack)中,速度较快
 
 
 
13.常常记得将不使用的对象设为null
 
 
 
14.尽量使用方法同步代替代码块同步(在使用同步机制时)
 
 
 
15.尽量减少对变量的重复计算
 
例如:for(int i = 0;i < list.size(); i ++) { // 每次循环都要计算一次list的size()
             …
}
应替换为:
for(int i = 0,int len = list.size();i < len; i ++) {
             …
}
 
 
 
16.用移位操作替代乘法和除法
 
 
 
17.array(数组) 和 ArryList的使用:尽可能使用array
array([]):最高效;但是其容量固定且无法动态改变;
ArrayList:容量可动态增长;但牺牲效率;
基于效率和类型检验,应尽可能使用array,无法确定数组大小时才使用ArrayList
 
 
 
18.尽量使用HashMap 和ArrayList ,除非必要,否则不推荐使用HashTable和Vector ,后者由于使用同步机制,而导致了性能的开销。
 
 
 
19.在循环外创建变量
 
 
 
20.用长度为0来判断字符串为空字符串
 
 
 
21.不要多此一举
 
StringBuffer buf = new StringBuffer();
buf.append("Hello").append(" ").append("World");
正确的做法:
StringBuffer buf = new StringBuffer();
buf.append("Hello World");
 
 
 
22.A Thread which is created without specifying a run method does nothing other than a delay in performance.
 
public void method() throws Exception
{
    new Thread().start();
}
正确的做法:
public void method(Runnable r) throws Exception
{
    new Thread(r).start();
}
 
 
 
23.Creation of constant immutable objects that are not assigned to static final variables lead to unnecessary memory consumption. 
 
public class Test
{
protected Object[] getObjects() 
{
        return new Object;
     }
 
    publicstatic Integer convertToInt(String s) 
    {
        if (s == null || s.length() == 0)
 {
            return new Integer(-1);
 }else {
            return new Integer(s);
  }
    }
}
修改为:
public class Test 
{
public static final Object[] NO_OBJECTS = new Object;
 
    protected Object[] getObjects() 
   {
        return NO_OBJECTS;
    }
 
    private static final Integer INT_N1 = new Integer(-1);
 
    public static Integer convertToIn(String s) {
        if (s == null || s.length() == 0)
{
            return INT_N1;
}else{
            return new Integer(s);
}
    }
}
 
 
 
24.如果计算可以放到循环外,就尽量放到循环外
 
 
 
25.避免重复else if的重复判断
 
 
 
26.使用entrySet()代替keySet()
 
public void method()
{
Map m = new HashMap();
Iterator it = m.keySet().iterator();
Object key = it.next();
Object v = m.get(key);
}
正确的做法:
public void method()
{
Map m = new HashMap();
Set set = m.entrySet();
Object keyValuePair = it.next();
}
附keySet使用实例:
public void someMethod(HashMap collection)
{
        Set keySet = collection.keySet();
        Iterator keyIter = keySet.iterator();
        while (keyIter.hasNext()) 
{
            keyIter.remove();
        }
}
 
 
 
27.移除一个集合的某一项
 
public void someMethod(Collection collection)
{
Iterator iter = collection.iterator();
while (iter.hasNext()) 
{
Object element = iter.next();
collection.remove(element); 
}
}
正确的做法:
public void someMethod(Collection collection)
{
Iterator iter = collection.iterator();
while (iter.hasNext()) 
{
iter.remove();
}
}
 
 
 
28. Do not declare members accessed by inner class private. 
 
public class Do_not_declare_members_accessed_by_inner_class_private_violation 
{
private int iVar = 0;
class inner
{
int var2;
public void foo()
{
var2 = iVar;
// ...
}
}
}
正确应该是:
public class Do_not_declare_members_accessed_by_inner_class_private_correction 
{
int iVar = 0;
class inner
{
int var2;
public void foo()
{
var2 = iVar;
// ...
}
}
}
 
 
 
29.Use BufferedInputStream and BufferedOutputStream or equivalent buffered methods wherever possible.
 
 
 
30.Use instanceof only on interfaces and avoid null check before checking instanceof. 
if (o instanceof MyInterface) { }
 
 
 
31.use .class instead of .getClass()
 
 
 
32.The inner class that doesn't require the outer class reference, should be declared static.
 
 
 
33.Instead of calling executeUpdate, use executeBatch. 
 
 
 
34.Use toArray(Object[]) instead of toArray() on collection.
 
 
 
35. 避免使用yield()方法,可使用wait()代替。
 
 
 
36.ArrayList的话初始化容量最好了,不过如果知道大小就不用它了,直接用Array了
 
 
 
37.优先使用DataSource获取Connection对象,而不是DriverManager
 
 
 
38.出于性能方面的考虑,尽量不要使用Thread.sleep(),可使用this.wait()代替。
 
 
 
39.Use char[] representation of the string instead of using String.charAt()
 
for(int i=0; i<str.length(); i++)
{
System.out.println(str.charAt(i));
}
比较正确的做法:
char[] carr = str.toCharArray();
for(int i=0; i<carr.length; i++)
{
System.out.println(carr);
}
 
 
 
40.避免使用java.lang.reflect包
 
Class c = Class.forName(classname);
Method m = c.getMethod("getData",null);
m.invoke(c.newInstance(), null);   
可优化为:
Class c = Class.forName(classname);
IDataProvider dataprovider = (IDataProvider) c.newInstance();
return dataprovider.getData(); 
 
 
 
 
 
还妄对尔等有些用处,如没有,仅当娱乐则已。(资料积累)
页: [1]
查看完整版本: Java编程40条