Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2087914
  • 博文数量: 413
  • 博客积分: 10926
  • 博客等级: 上将
  • 技术积分: 3862
  • 用 户 组: 普通用户
  • 注册时间: 2006-01-09 18:14
文章分类

全部博文(413)

文章存档

2015年(5)

2014年(1)

2013年(5)

2012年(6)

2011年(138)

2010年(85)

2009年(42)

2008年(46)

2007年(26)

2006年(59)

分类: Java

2009-03-04 12:51:16

  1. Release memory
    All objects in Java are pointer, and they are release by GC (Garbage Collector) automatically. If you want to release it explicitly, you can use the following 2 method:
            obj = null;
    or
            obj.finalize();
  2. Create an instance of array of array
    int[][] arrays;
    arrays = new int[10][20];
    arrays[0][1] = 1;
    ....
    arrays[9][19] = 100;
  3. Define temporary class then instance it.
    //A.java
    public abstract class A
    {
        public A()
        ........
        abstract void Func();
    };
    //B.java
    // Define a temporary class derived from A to overload some member functions
    // then instance it.
    A obj = new A()
    {
        void Func()
        {
           ........
        }
    };
  4. unsigned number
    Java doesn't support unsigned number,but in prictice, we can use other data
    byte to completely replace those unsigned number:
        unsigned byte -> short/char
        int -> unsigned short or unsigned char
        long -> unsigned int.
    For example:
        short a = (xxxxxx & 0xff); //unsigned byte
        int a = (xxxx & 0xffff); //unsigned short
        long a = (xxxxxx & 0xffffffffL); //unsigned int
  5. Arbitaray number of arguments
    1. Return value from arguments
      1. Object, only data member of Object can be changed in the method.
          b) type[] param
        void fun(byte[] param); param can be changed in the method.
    2. ...
  6. Arrays
    1. Simple data type array
          type[] array = {x, x, x};
          type[] array = new type[NUM];
      i.e:
          int[] arrayInt = {1, 2, 3, 4};
          float[] arrayFloat = new floag[10];
          arrayFloat[0] = 10.0;
          arrayFloat[1] = 20.0; 
          ....
    2. Class array
          Class xx1, xx2, xx3;
          Class[] arrayObj = {xx1, xx2, xx3};
      or
          Class[] arrayObj = new Class[NUM];
          arrayObj[0] = new Class():
          arrayObj[1] = new Class():
          arrayObj[2] = new Class():
          ...
      i.e:
          String[] arrayString = new String[10]
          arrayString[0] = new String("Str 0");
          arrayString[1] = new String("Str 1");
          arrayString[2] = new String("Str 2");
         ......
      or
          String[] arrayString = new String[10]
          {
             "str 0", "str 1", ......
          };
  7. java 多线程设计wait、notify、notifyall、synchronized的使用机制
        synchronized(obj)
        {
            while(!condition)
            {
                obj.wait();
            }
            obj.doSomething();
         }
        当线程A获得了obj锁后,发现条件condition不满足,无法继续下一处理,于是线程A就wait() , 放弃对象锁.
        之后在另一线程B中,如果B更改了某些条件,使得线程A的condition条件满足了,就可以唤醒线程A:
        synchronized(obj)
        {
            condition = true;
            obj.notify();
        }
    需要注意的概念是:
    1.  调用obj的wait(), notify()方法前,必须获得obj锁,也就是必须写在synchronized(obj) {…} 代码段内。
    2. 调用obj.wait()后,线程A就释放了obj的锁,否则线程B无法获得obj锁,也就无法在synchronized(obj) {…} 代码段内唤醒A。
    3. 当obj.wait()方法返回后,线程A需要再次获得obj锁,才能继续执行。
    4. 如果A1,A2,A3都在obj.wait(),则B调用obj.notify()只能唤醒A1,A2,A3中的一个(具体哪一个由JVM决定)。
    5.  obj.notifyAll()则能全部唤醒A1,A2,A3,但是要继续执行obj.wait()的下一条语句,必须获得obj锁,因此,A1,A2,A3只有一个有机会获得锁继续执行,例如A1,其余的需要等待A1释放obj锁之后才能继续执行。
    6. 当B调用obj.notify/notifyAll的时候,B正持有obj锁,因此,A1,A2,A3虽被唤醒,但是仍无法获得obj锁。直到B退出 synchronized块,释放obj锁后,A1,A2,A3中的一个才有机会获得锁继续执行。
    7. ...
  8. Inner class
    public class Foo {
       
    private int mValue;

       
    public void run() {
           
    Inner in = new Inner();
            mValue
    = 27;
           
    in.stuff();
       
    }

       
    private void doStuff(int value) {
           
    System.out.println("Value is " + value);
       
    }

       
    private class Inner {
           
    void stuff() {
               
    Foo.this.doStuff(Foo.this.mValue); //Access method of other class
           
    }
       
    }

        public
    class InnerB {
           
    void stuff() {
               
    Foo.this.doStuff(Foo.this.mValue); //Access method of other class
           
    }
       
    }

    }

    Refer to inner class in code:
    Foo.InnerB,
    Refer to inner class in jni: Foo$InnerB.
  9. finalize()调用的时机
    • 所有对象被Garbage Collection时自动调用,比如运行System.gc()的时候.
    • 程序退出时为每个对象调用一次finalize方法。
    • 显式的调用finalize方法
    除此以外,正常情况下,当某个对象被系统收集为无用信息的时候,finalize()将被自动调用,但是jvm不保证finalize()一定被调用,也就是说,finalize()的调用是不确定的,这也就是为什么sun不提倡使用finalize()的原因.
  10. How to invoke a private method of one class
    Suppose you want to invoke the methos privateMethod() of class BusClass, and busObj is the instance of BusClass, you can follow the steps belose to invoke the private:
    Class c = Class.forName(busObj.getClass().getName());
    Method m = c.getDeclaredMethod("privateMethod");
    m.setAccessible(true);
    (BusClass)m.invoke(busObj);
  11. Get byte order
    import java.lang.ByteOrder;
    if (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDINE)
    {
        System.out.println("big endine");
    }
    else if (ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDINE)
    {
        System.out.println("little endine");
    }
  12. Collection
    • Enumeration
    • List
      1. ArrayList
        Vector is like ArrayList, but Vector is sychronized.
      2. LinkedList
    • Map
      1. HashTable
      2. HashMap
      3. EnumMap
    • Set
      1. HashSet
      2. EnumSet
    • xxx
  13. Run process and get output
    try
    {
        String line;
        Process p = Runtime.getRuntime().exec("logcat");
        BufferedReader input = new BufferedReader (new InputStreamReader(p.getInputStream()));
        while ((line = input.readLine()) != null) //If no data is available, this function will hang until the process is terminated.
        {
            System.out.println(line);
        }
        input.close();
    }
    catch (Exception err)
    {
        err.printStackTrace();
    }
  14. ...

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