標籤

二元樹 (1) 字串常數池 (1) 投資 (3) 每月損益 (37) 例外處理 (1) 泛型 (2) 股票 (15) 指標 (5) 英文 (8) 基本資料型別 (1) 期貨 (1) 程式交易 (10) 量化投資 (5) 亂亂寫 (3) 概念 (3) 資料結構 (3) 演算法 (3) 數學 (3) 轉型 (1) AMA (1) ArrayList (1) assert (1) BeautifulSoup (1) C/C++ (8) casting (1) ClassCastException (1) classpath (1) Collection (4) Comparable (1) comparTo() (1) constructor (1) database (3) Debian (1) Dropbox (2) EA (2) enum (1) equals() (2) exception (3) extends (1) ffmpeg (1) final (1) Git (1) HashMap (1) HashSet (1) hasNext() (1) HTS (3) instanceof (1) IS-A (1) Iterator (1) JAVA (43) length (1) Linux (31) List (1) Mac (6) Map (1) ML (2) MT4 (6) MySQL (2) next() (1) NullPointerException (1) Number (1) Numpy (2) OpenCart (1) OpenCV (3) OSX (1) overloading (1) overriding (3) pandas (2) PHP (8) PriorityQueue (1) Python (11) Queue (1) random() (1) reverse() (1) Samba (1) SCJP (21) sqrt() (1) synchronized (1) talib (1) ufw (1) uTorrent (1) var-args (2) VHF (1) vim (2) Yhoo知識+ (4)

2012年10月22日 星期一

synchronized


  • 只有函式或區塊可以標上synchronized
    public synchronized void go(){ //code }    //函式
    pubic void go(){ synchronized(this){ //code } }    //區塊
  • 區塊標示法為 synchronized(物件)或synchronized(類別.class)
    synchronized(o)
    synchronized(this)
    synchronized(MyClass.class)

日期格式


  • java.util.Date:
    Date d = new Date();    //建立目前時間的實體
  • java.util.Calender:Calendar為抽象類別,須用靜態函式建立物件
    Calendar c = new Calendar.getInstance();
    c.setTime(d);    //將Date物件指派給c物件
    d = c.getTime();    //將Calendar物件c所設定的時間指派給Date物件d
  • java.util.DateFormat:DateFormat為抽象類別,須用靜態函式建立物件
    DateFormat df = DateFormat.getInstance();
  • java.util.Locale:
    Locale loc = Locale.getDefault();    //取設當地預設格式

2012年10月21日 星期日

關於static


  1. static 函式或變數是屬於類別,被JVM載入時即存在;在物件生成前即存在。
  2. static 函式可透過類別或繼承直接呼叫,或是可用參考變數呼叫:
    class Foo{ static int count = 5; }
    class Abc extends Foo{
        public static void main(String[] args){
            Foo f = new Foo();
            System.out.println(count);
            System.out.println(Foo.count);  
            System.out.println(f.count);    //編譯器是看f的型別(Foo)來呼叫static成員
        }
    }
    //輸出皆為5
  3. static 函式不能存取實體變數或函式(即非static之實體成員)。
  4. static 函式不能被覆寫(overriding)。

函式配對

void go(long n){ System.out.println("long"); }
void go(double n){ System.out.println("double"); }
void go(Integer n){ System.out.println("Integer"); }

int i = 5;
go(5);


output: long

1、函式會依下列參數型別的順序尋找最適合之配對函式:
widening > autoboxing > var-args

2、其中widening(基本資料放寬)會依下列參數型別順序尋找配對:
byte > short > int > long > float > double

3、其中autoboxing(自動包裝成wrapper物件)如下:
boolean --> Boolean
byte --> Byte
short --> Short
char --> Character
int --> Integer
long --> Long
float --> Float
double --> Double

*基本資料可放寬,但wrapper物件不能放寬。Integer無法放寬成Long
void go(Long n){ System.out.println("Long"); }
go(new Integer(5));      //不會放寬

2012年10月17日 星期三

Modifier的可視範圍

private        class

default        class package


protected      class package extends


pubic          any




存取其class成員:
private     同一class裡
default     承上或同一package裡
protected  承上或繼承
public      皆可

2012年10月1日 星期一

toString測試


public class ToStringTest{
public static void main(String[] args){
String x = "big surprise";
int[] y = {1,3};
ToStringTest z = new ToStringTest();

System.out.println(x.toString()+ "\n" +y.toString()+ "\n" +z.toString());
}
}



$ java ToStringTest
big surprise
[I@9b49e6
ToStringTest@10d448



2012年9月29日 星期六

exception測試2

class BadFoodException extends Exception{ }
public class MyException{
    static String[] food = {"Apple", "Banana", "Cocoa", "Coffee", "Toast"};

    public static void main(String[] args){
        try{
            checkFood(args[0]);
        }catch(BadFoodException e){
            System.out.println("I don't like.");
        }
    }
   
    static void checkFood(String input) throws BadFoodException{
        int flag = 0;
        for(String s : food){
            if(s.equals(input)){
                flag = 1;
                break;
            }
        }
        if(flag == 1)    System.out.println("I like it.");
        else throw new BadFoodException();
    }
}



==================================
$ java MyException Toast
I don't like.

$ java MyException Apple
I like it.