Chinaunix首页 | 论坛 | 博客
  • 博客访问: 334930
  • 博文数量: 89
  • 博客积分: 5152
  • 博客等级: 大校
  • 技术积分: 1155
  • 用 户 组: 普通用户
  • 注册时间: 2006-02-25 15:12
文章分类

全部博文(89)

文章存档

2012年(1)

2011年(5)

2010年(14)

2009年(69)

我的朋友

分类: Java

2010-04-02 16:28:17

翻译自官方文档,有几条吃不准就没翻译,如果您有自己的理解,请留言,谢谢



Best practice: Writing efficient code
To allow a BlackBerry® Java® Application to use resources efficiently, consider the following guidelines:

• Use local variables.
  尽可能使用局部变量,访问局部变量比访问类成员变量效率更高。

• Use shorthand for evaluating Boolean conditions.
  To evaluate a Boolean condition, use shorthand. The resulting compiled code is shorter.
  我的理解:直接使用表达式判断,而不需要一个boolean型的中间变量
  Code sample
   return( boolean_expression );

• Make classes final.
  如果你知道你的class永远不会被继承,把它标记为final,final关键字让编译器编译出更有效率的代码。

• Use int instead of long.
  Java中,long是64位整数,而BB是32位机,所以使用int比使用long快2-4倍。

• Avoid garbage collection.
  因为BB的内存有限,所以尽量避免调用System.gc(),让Java虚拟机自己来做。

• Use static variables for Strings.
  当你定义一个static String时,不使用final比使用final的速度更快,对于其他的基本数据类型则相反,
  比如说int类型。例如:private static final String x ="example";
  当你每次使用变量x时,都会创建一个临时的String实例,在字节码中,编译器把所有的x替换成“example”
  所以每次当你引用x的时候,JVM都会到一个hash table中查询一次。
  相反,当你创建一个非final的static String时,JVM只会在初始化x时到hash table中查询一次,所以访问
  速度更快:private static String x = "example";
 

• Avoid the String(String) constructor.
  每个String(string)都会创建一个java.lang.String class实例,所以避免使用String(string)构造函数。
  例如:
    String str = "abc";
    String str = "found " + n + " items";

• Use a final or non-final static String, depending on your application's requirements.
  non-final static String需要更多的内存,final static String占用更多cpu时间。
  根据你的程序需要进行调整。

• Write efficient loops.
  如果你的容器中包含一个或一个以上的元素,把容器的大小赋给一个局部变量。
      int size = vector.size();
    for( int i = 0; i < size; ++i ) {
        ...
    }
  如果你容器中元素的访问顺序不重要,那么可以倒序访问,这样可以避免创建额外的局部变量。
    for( int i = vector.size() - 1; i >= 0; --i ) {
        ...
    }


• Optimize subexpressions.
  如果你使用同一个表达式两次或以上,那么使用局部变量代替它。
    int tmp = i+1; one( tmp ); two( tmp );

• Optimize division operations.
  除法在BB上可能会变得很慢,因为BB处理器中没有除法指令集。
  如果你明确的知道,你在对两个 正数 做除法运算,可以使用位移指令。
  例如:int = width >> 1;

• Avoid java.util.Enumeration.
  Avoid using java.util.Enumeration objects unless you want to hide data (in other  words, to return an enumeration of the data instead of the data itself). Asking a vector or hash table for an Enumeration object is slow and creates unnecessary
 garbage. If another thread might modify the vector, synchronize the iteration. The Java® SE uses an Iterator object for similar
 operations, but Iterator objects are not available in the Java® ME.

 Code sample
 for( int i = v.size() - 1; i >=0; --i ) {
    o = v.elementAt( i );
    ...
 }
synchronized( v ) {
for( int i = v.size() - 1; i >=0; --i ) {
o = v.elementAt( i );
...
}}

• Perform casts using instanceof.
  使用instanceof来验证类型转换是否成功。
  例如:
    if( x instanceof String ) {
        (String)x.whatever();
    } else {
        ...
    }

• Evaluate conditions using instanceof.
  当你判断一个变量是否为null时,最好使用instanceof来判断。
  例如:
    if( e instanceof ExampleClass ) { ... }
    if( ! ( e instanceof ExampleClass ) ) { ... }

• Avoid using StringBuffer.append (StringBuffer).
  当append一个String buffer到另外一个时,我们应该调用bb的api(理由呢?)
    net.rim.device.api.util.StringUtilities.append( StringBuffer dst, StringBuffer src[, int offset, int length ] ).
  例如:
    public synchronized StringBuffer append(Object obj) {
        if (obj instanceof StringBuffer) {
            StringBuffer sb = (StringBuffer)obj;
            net.rim.device.api.util.StringUtilities.append( this, sb, 0, sb )
            return this;
        }   
        return append(String.valueOf(obj));
    }

• Avoid returning null.
  如果你不希望返回null,那最好抛出一个恰当的异常,这样可以让调用者显式的来处理这个异常

• Avoid passing null into methods.
  不要传null给API,除非JDE明确说明支持null参数。

• Use caution when passing null into a constructor.
  当给一个构造函数传null的时候,一定要注意,因为一个class可能有多个构造函数,如果你以null作为参数来
  构造函数,可能会带来二义性,编译器可能会报错

• Use long for unique identifiers.
  Use a long identifier instead of a String identifier for unique constants, such as GUIDs, hash table keys, and state or context
  identifiers.For identifiers to remain unique across a BlackBerry® Java Application, use keys that an application generates based
  on a hash of a String. In the input String, include enough information to make the identifier unique. For example, use a fully
  qualified package name such as com.rim.samples.docs.helloworld.

• Exit applications correctly.
  在你调用System.exit(int status)之前,一定要做好清理工作。

• Print the stack trace.
  为了调试程序,你可以进行堆栈跟踪
  例如:
    catch (Throwable t) {
        t.printStackTrace();
    }



阅读(656) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~