今年夏天,sun将计划发布一个java编程语言的重大修改版。该版本代号取名为“tiger”,然而,很快将会有jdk1.5的官方名称。
该版本的java语言将合并java规格提案(java specification requests,jsr)14与15 (jsr-14, jsr-175)。它还将在运行时性能(runtime performance)、可扩缩性(scalability)、易管理性(manageability)和监控(monitoring)方面有较大加强。
本文中,我们将讨论几个在jdk1.5中新的语言特征,包括:
泛型(generics)--为集合(collections)提供编译时类型安全,无需每刻从collections取得一个对象就进行强制转换(cast)
增强的“for”循环(enhanced for loop)--减少迭代器(iterator)的潜在错误(error-proneness)
自动置入/自动取出(autoboxing/unboxing)--无需在基本类型(primitive types)(例如double)和包装类型(wrapper types)(例如double)之间人工地进行转换。
类型安全的枚举(typesafeenums)--提供类型安全枚举模式的各项好处。
静态导入(static import)--无需在使用其他类的静态成员变量前缀其类名.这将使得代码更为简洁。
元数据(metadata)--使编程人员避免编写样板化代码(boiler plate code),并提供机会进行宣告式程式设计(declarative programming)。
让我们详细讨论每个新特性,并看一些例子。
泛型(generics)
泛型是jdk1.5中一个最“酷”的特征。通过引入泛型,我们将获得编译时类型的安全和运行时更小地抛出classcastexceptions的可能。在jdk1.5中,你可以声明一个集合将接收/返回的对象的类型。在jdk1.4中,创建雇员名字的清单(list)需要一个集合对象,像下面的语句:
list listofemployeename = new arraylist();
在jdk1.5中,你将使用下面语句
list
最“酷”的是,如果你试图插入非string类型的值,你将在编译时发现并且修正这类问题。没有泛型,你会发现这样一个bug,当你的客户调用后会告诉你,你所编写的程序抛出classcastexception异常而崩溃。
另外,当你从集合中得到一个元素时你无需进行强制转换。故原先为:
string employeename = ((string) listofemployee.get(i));
而下面的语句将比上面的更加简单:
string employeename = listofemployee.get(i);
不清楚对象的类型而强制转换对象是不合理的,并且更重要的是,它将在运行时失败。假使用户无意间传入一个包含string buffers类型而非string类型的集合,那结果会怎样呢。在listing a中,客户被要求传入一个编译器无法强制的strings类型集合。listing b中显示了同样的方法使用泛型是如何实现的。
listing a
staticbooleancheckname(collection employeenamelist, string name) {
for (iteratori = employeenamlist.iterator(); i.hasnext(); ) {
string s = (string) i.next();
if(s.equals(name)){
return true;
//print employee name here ......
}
}
return false;
}
listing b
staticbooleancheckname(collection
for (iteratori = employeenamlist.iterator(); i.hasnext(); ) {
if(i.next().equals(name)){
return true;
//print employee name here ......
}
}
return false;
}
现在,通过方法签名可以清楚知道输入集合必须只能包含strings。如果客户试图传入一个包含string buffers的集合,程序将不会编译。同时注意,该方法不包含任何强制转换。它只需要短短一行,一旦你习惯泛型后,它也更加清晰。
增强的for循环
在jdk当前版本下的for循环语法如下:
void printall(collection c) {
for (iteratori = c.iterator(); i.hasnext(); ) {
employee emp = (employee)i.next();
system.out.println(emp.getname());
}
}
现在,用增强的for语句实现相同方法:
voidprintall(collection c) {
for (object o : c)
system.out.println((timertask)o).getname());
}
在这类for循环中,你应该将":"看成"in",所以,在该例中可以看成"for object o in c"。你可以发现这种for循环更具可读性。
自动置入/自动取出(autoboxing/unboxing)
java有基本数据类型,在这些基本数据类型周围又有包装类。通常,编程人员需要将一种类型转换成另一种。看看listing c.中的代码片断。
listing c
public class employee {
private static final integer child = new integer(0);
public static void main(string args[]) {
//code for adding n to an integer
int n=10;
integer age= new integer(30);
integer ageaftertenyear= new integer(age.intvalue +10);
}
}
请注意,用于计算ageaftertenyear的内循环代码看上去是多么杂乱。现在,在listing d.中看看相同的程序使用autoboxing重写后的样子。
listing d
public class employee {
public static void main(string args[]) {
int n=10;
integer age= new integer(30);
integer ageaftertenyear= age +10;
}
}
有一件事值得注意的:在先前,如果你取出(unbox)null值,它将变为0。在次代码中,编译器将自动地转换integer为int然后加上10,接着将其转换回integer.。
类型安全的枚举(typesafeenums)
类型安全枚举提供下列特性:
他们提供编译时类型安全。
他们都是对象,因此你不需要将他们放入集合中。
他们作为一种类的实现,因此你可以添加一些方法。
他们为枚举类型提供了合适的命名空间。
他们打印的值具有情报性(informative)― 如果你打印一个整数枚举(intenum),你只是看见一个数字,它可能并不具有情报性。
例一:
enum season { winter, spring, summer, fall }
例二:
public enum coin {
penny(1), nickel(5), dime(10), quarter(25);
coin(int value) { this.value = value; }
private final int value;
public int value() { return value; }
}
静态导入(static import)
静态导入使代码更易读。通常,你要使用定义在另一个类中的常量(constants),像这样:
importorg.yyy.pkg.increment;
class employee {
public double calculatesalary(double salary{
return salary + increment.increment * salary;
}
}
当时使用静态导入,我们无需为常量名前缀类名就能使用这些常量,像这样:
import static org.yyy.pkg.increment;
class employee {
public double calculatesalary(double salary{
return salary + increment * salary;
}
}
注意,我们可以调用increment这一常量而不要使用类名increment.。
元数据(metadata)
元数据特征志于使开发者们借助厂商提供的工具可以进行更简易的开发。看一看listing e.中的代码。
listing e
importorg.yyy.hr;
public interface employeei extends java.rmi.remote {
public string getname()
throwsjava.rmi.remoteexception;
public string getlocation ()
throwsjava.rmi.remoteexception;
}
public class employeeimpl implements employeei {
public string getname(){
}
public string getlocation (){
}
}
通过元数据的支持,你可以改写listing e中的代码为:
importorg.yyy.hr;
public class employee {
@remote public string getname() {
...
}
@remote public public string getlocation() {
...
}
}
正像你所看到的,所有样板化的代码都不见了。
这些新特性和规格说明将在jdk1.5中实现。它将提供java编程社区更多的选择以编写鲁棒的、可扩展的代码。认真的java编程
闽公网安备 35060202000074号