2004年下半年,Sun公司發布了開發代號為“Tiger”的J2SE 5.0,揭開了Java發展的一個重要裡程碑。在過去的Java升級中更多的是進行一些庫函數的改進,而這次則直接從文法層面上進行了增強。直接從1.4跳到5.0(Sun本來是打算用1.5.0這個版本號碼的),到底有什麼改變呢.
1. Metadata (中繼資料)
能讓你在程式中嵌入註解(annotation),這些註解就可以被不同的編程工具處理,產生java 原始碼. 因此只需要指定一種動作.實際的事情留給工具來處理.
sample
Before
public interface PingIF extends Remote {
public void ping() throws RemoteException;
}
public class Ping implements PingIF {
public void ping() {
}
}
After
public class Ping {
public @remote void ping() {
}
}
java5.0 中提供了一個apt的工具(annotation processing tool) 可以從有中繼資料的java class 或是類檔案中產生新java source code 同時還可以編譯原始的java程式和產生的程式
2. Generic Types (泛型)
java5.0中使用最多的是在集合類中, 在5.0之前.集合類只提供一種類型(object 類型)儲存. 而現在可以提供多種資料類型,具體文法有點像C++中的模板類.
sample
before
ArrayList list = new ArrayList();
list.add(0, new Integer(42));
int total = ((Integer)list.get(0)).intValue();
after
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(0, new Integer(42));
int total = list.get(0).intValue();
因此。在以前需要做casts 而現在不需要了。
3. Autoboxing and Auto-Unboxing of Primitive Types(基本類型的自動拆箱,裝箱)
對於像int, boolean,double等基本類型,java5.0中會當放放集合類型中時自動為其裝箱成相應的對像如Integer,Boolean,Double資料類型,當取出時自動拆箱成相應的基礎資料型別 (Elementary Data Type)
sample
before
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(0, new Integer(42));
int total = (list.get(0)).intValue();
after
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(0, 42);
int total = list.get(0);
4. Enhanced for Loop (增強型迴圈語句)
在以前使用最多的是Iterator class 該類提供對集合元素的遍曆訪問. 在java 5.0中增強型的迴圈語句允許直接對集合內元素進行遍曆.
sample
before
ArrayList<Integer> list = new ArrayList<Integer>();
for (Iterator i = list.iterator(); i.hasNext();) {
Integer value=(Integer)i.next();
}
after
ArrayList<Integer> list = new ArrayList<Integer>();
for (Integer i : list) { ... }
5. Static Import (靜態常量匯入)
使用 " import static" 關鍵字,讓你能夠直接匯入類中的靜態常量,而不需要從該類中繼承
sample
before
import java.awt.BorderLayout;
getContentPane().add(new JPanel(), BorderLayout.CENTER);
after
import static java.awt.BorderLayout.*;
getContentPane().add(new JPanel(), CENTER);
6. Formatted Output(格式化輸出)
能夠讓你用printf語句格式化輸出. 對於Data和BigInteger類型的格式化規則可以參照java.util.Formatter 類. 這使得你原先C的程式在printf中不南要做任何的改變
sample
System.out.printf("name count%n");
System.out.printf("%s %5d%n", user,total);
注意,雖然在Unix環境下也支援使用n 做為斷行符號換行,但是為了使程式能夠跨平台.在java中使用%n 來替代.
7. Formatted Input(格式化輸入)
使用scanner API 來支援對讀入資料的格式化, 如果要使用更為複雜的格式.可以使用java.util.Formatter 類中的Regex.
sample
Scanner s= new Scanner(System.in);
String param= s.next();
int value=s.nextInt();
s.close();
8.Varargs (變參數)
變參數,允許使用不定參數做為函數的參數,在以前則需要使用數組來傳遞
sample
void argtest(Object ... args) {
for (int i=0;i <args.length; i++) {
}
}
argtest("test", "data");
9.Concurrency Utilities ( 線程同工具包)
在java5.0中加入JSR-166中對線程同步的工具包,以支援更為強大,更高層的線程操作(運行線程本身,和起動線程). 提供了強大的線程任務管理架構,安全執行緒隊列,定時器,鎖(包括原子鎖:注此處為小粒度的鎖,以前只能使用對方法和對像加鎖) 該包存在於java.util.concurrent
sample
final private Semaphore s= new Semaphore(1, true);
s.acquireUninterruptibly(); //for non-blocking version use s.acquire()
try {
balance=balance+10; //protected value
}finally {
s.release(); //return semaphore token
}
10. rmic -- The RMI Compiler( RMI 編譯器)
java5.0中不需要再使用rmic工具來產生遠程介面的stubs,而是通過在運行時自動的發現(動態代理)來實現.
11. Scalability and Performance(可測量性和效能)
java5.0中承諾通過增加新的起動時間和運行期記憶體的跟蹤功能來改進了java的可測量性和效能.
12. Monitoring and Manageability(監控和管理)
java5.o中提供了對JVM的監控和管理,通過使用JMX(java management extensions)架構和遠端連線協議來對JVM進行監控和資料觀查與測量
13 其它改進
a. New JVM Profiling API (JSR-163)
b. Improved Diagnostic Ability
c. Desktop Client(外觀)
d. Core XML Support
e. Supplementary Character Support ( unicode 4 support)
f. JDBC RowSets (CachedRowSet, WebRowSet)
參考 : http://java.sun.com/features/2003/05/bloch_qa.html