`

今天看到关于Java 7代码书写上的一些方便的新特性

阅读更多
以下文章的中文部分是我自己的一点小评价,它们中的一些也许不会出现在正式的Java 7当中,因为有的目前还只是提案而已(尽管提案被通过的机会很大)。

Looking forward to Java 7

Dolphin will not be released in 2007. Next year is a more realistic goal. That said, work is underway and some features may make their debut as standard extensions earlier, or at least as betas.

我也觉得必须要留够时间给Mustang来折腾折腾啊,要是弄得和Tiger一样短命且孤芳自赏的话,SUN和一干Java幕后势力的面子也不好看哟。

It's unfortunate that it's far, far easier to add features to a language than to take them away. Almost inevitably, languages get more complex and confusing over time, not less. Even features that look good in isolation become problematic when piled on top of each other.

Unfortunately, the Java community has not yet learned this lesson, despite the debacle that is generics. There's just something about new syntax that's too cool and exciting for language designers to resist, even when it doesn't solve any actual problems. Thus the huge clamor for new language features in Java 7, including closures, multiple inheritance, and operator overloading.

I suspect that before the year is out we will see closures in a Java 7 beta, and we may well see operator overloading (50/50 chance), but multiple inheritance simply will not happen. Too much of Java is based on a singly rooted inheritance hierarchy. There's no plausible way to retrofit multiple inheritance into the language.

Currently there's a lot of syntax sugar on the table, some of which makes sense, some of which doesn't. A lot of the proposals focus on replacing methods like getFoo() with operators like ->.

The first possibility is using array syntax for collections access. For example, instead of writing this:

List content = new LinkedList(10);
content.add(0, "Fred");
content.add(1, "Barney");
String name = content.get(0);

you could instead write this:

List content = new LinkedList(10);
content[0] = "Fred";
content[1] = "Barney";
String name = content[0];

Another possibility is allowing array initializer syntax for lists. For example:

LinkedList content = {"Fred", "Barney", "Wilma", "Betty"}
这样的类似于数组的各项对集合的操作的写法可以说是很友好的形式,可以另人更加直观的看到赋值和取值的语句。这个对那些很熟悉C语言的人事来说应该是一件好事情,就是不知道集合的操作速度会不会受到影响……

Both of these proposals could be implemented with a little compiler magic without changing the virtual machine (VM), an important characteristic for any revised syntax. Neither proposal would invalidate or redefine any existing source code, an even more important issue for new syntax.

One language feature that might make a real difference in developers' productivity would be built-in primitives for managing tables, trees, and maps, such as you encounter when working with XML and SQL. Projects like E4X in JavaScript land and Cω and Linq in the Microsoft world are pioneering this idea, but sadly, the Java platform seems to be missing the boat. If anyone feels like making a potential game-saving play by forking the compiler, here is a very good place to look.

We'll probably also see some syntax sugar for property access. One proposal is to use -> as a shorthand for calling getFoo and setFoo. For example, instead of writing:

Point p = new Point();
p.setX(56);
p.setY(87);
int z = p.getX();

you could write"

Point p = new Point();
p->X = 56;
p->Y = 87;
int z = p->X;

Other symbols including . and # have also been proposed in lieu of ->.

In the future, you may or may not have to explicitly identify the relevant field as a property in the Point class, like so:

public class Point {

    public int property x;
    public int property y;

}
是的,只要类当中定义的属性对于外部的对象是可见的话,就完全不必再书写getter和setter方法来获取它们了!这样一来,势必可以大量减少书写getter,setter方法在代码中说占据的篇幅。对于经常编写pojo的同志来说,这个应该是个便利的特性。

Personally, I'm underwhelmed. I'd like to see the Java platform adopt a more Eiffel-like approach in which we could actually use public fields. However, if getters or setters are defined with the same names as the fields, then reads and writes to the fields are automatically dispatched to the methods instead. It uses less syntax and it's more flexible.

Another proposal to replace methods with operators aims at BigDecimal and BigInteger. For example, currently you have to code unlimited precision arithmetic like so:

BigInteger low  = BigInteger.ONE;
BigInteger high = BigInteger.ONE;
for (int i = 0; i < 500; i++) {
    System.out.print(low);
    BigInteger temp = high;
    high = high.add(low);
    low = temp;
};
This could more clearly be written as:
 
BigInteger low  = 1;
BigInteger high = 1;
for (int i = 0; i < 500; i++) {
    System.out.print(low);
    BigInteger temp = high;
    high = high + low;
    low = temp;
};
我想这个可以算是Java 5加入autoboxing以后的又一次在代码便利性上面的进展吧!这样的写法让代码的易读性变得更高,相信也是受到了Ruby等新势力的影响所造成的吧。
 
The proposal seems inoffensive enough, though it could possibly lead to overuse of these classes and consequent performance degradation in naive code.

Java 7 could fix the most long-standing source of irritation to Java developers: the various class loaders and associated classpaths. Sun is taking another whack at this problem with the Java Module System. Instead of a .jar file, data will be stored in a .jam file. This is a sort of "superjar" that contains all the code and metadata. Most importantly, the Java Module System will support versioning for the first time, so you can say that a program needs Xerces 2.7.1 but not 2.6. It will also allow you to specify dependencies; for instance, to say that the program in the JAM requires JDOM. It should also enable you to load one module without loading them all. Finally, it will support a centralized repository that can provide many different versions of many different JAMs, from which applications can pick the ones they need. If the JMS works, jre/lib/ext will be a thing of the past. 

(Jam 也有果酱的意思,不知道这个甚至可以支持版本化的"superjar"能不能得到大家的认同。在我看来,它确实有够前卫的啊!)

I'm also hopeful that Java 7 will relax access restriction just a bit. It may become possible for subpackages to see the package-protected fields and methods of classes in their superpackages. Alternately, it may be possible for subpackages to see the package-protected members of superpackages that explicitly declare their friendliness. Either way, this would make dividing an application into multiple packages much simpler and dramatically improve testability. As long as unit tests were in a subpackage, you wouldn't have to make methods public to test them.

(包的作用将会在Java 7中会有一些变化/改进?这样一来,对象在方法和属性在声明的时候兴许能够变得灵活一些……)

Filesystem access has been a major problem for the Java platform since 1995. More than 10 years later, there's still no reliable cross-platform way to perform basic operations like copying or moving files. Fixing this has been an open issue for at least the past three JDKs (1.4, 5, and 6). Sadly, boring but necessary APIs for moving and copying files have been shunted aside in favor of less common but sexier operations like memory-mapped I/O. Possibly JSR 203 will finally fix this and give us a plausible, cross-platform file system API. Then again, the working group may once again spend so much time on the relatively unimportant problem of true asynchronous I/O that the filesystem gets left at the altar once again. We should know by this time next year.

(对文件系统的操作看来这次也是一个比较关注的事宜——如果真的能够做到在跨平台之后无需改变负责I/O部分的代码的话,我想无论是对于Java EE 还是Java桌面应用乃至Java ME都是利好的消息。)

Experimentation

Whatever changes are made, it would be nice if they could be implemented in open source forks first, so we can see just how much or how little difference they really make. Toward this end, Sun's Peter Ahè has started the Kitchen Sink Project on java.net. The goal is to branch and fork the javac compiler repeatedly to test many different ideas like these. Blogging about pet features is one thing; actually producing running code is something else entirely.

分享到:
评论
2 楼 justjavac 2009-10-03  
1 楼 herryhcm 2008-12-19  

相关推荐

    俄罗斯方块游戏(Java)

    该俄罗斯方块游戏是基于Java开发,借鉴了MVC的设计模式,通过建立模型类、游戏规则类和画图类、使程序流程更加清晰,并且程序具有强内聚、松耦合的特性,保证了功能扩展的方便性。另外该游戏对于开发类似的GUI游戏有...

    最新Java面试宝典pdf版

    11、有数组a[n],用java代码将数组元素顺序颠倒 80 12.金额转换,阿拉伯数字的金额转换成中国传统的形式如:(¥1011)-&gt;(一千零一拾一元整)输出。 81 三. html&JavaScript&ajax部分 82 1. 判断第二个日期比第一...

    window环境下的Java的jdk1.8

    现在开发用的最多的就是jdk...增加新特性Lambda表达式的内部类改造,使得代码在书写上变得更加简洁 3、强大的 Stream API 增加了核心功能,使得代码调用方式变得更加简洁 4、便于并行 5、最大化减少空指针异常 Optional

    Java面试宝典2012新版

    11、有数组a[n],用java代码将数组元素顺序颠倒 80 12.金额转换,阿拉伯数字的金额转换成中国传统的形式如:(¥1011)-&gt;(一千零一拾一元整)输出。 81 三. html&JavaScript;&ajax;部分 82 1. 判断第二个日期比第...

    [java]读书笔记整理:一切都是对象

    缩排格式使java代码易于阅读。由于java是一种自由格式(free-form)的语言,所以,空格,制表符,换行都不会影响程序的执行结果。 注意,尽管一下代码在C和C++中是合法的,但是在java中却不能这样书写: { int ...

    Java面试宝典-经典

    11、有数组a[n],用java代码将数组元素顺序颠倒 80 12.金额转换,阿拉伯数字的金额转换成中国传统的形式如:(¥1011)-&gt;(一千零一拾一元整)输出。 81 三. html&JavaScript&ajax部分 82 1. 判断第二个日期比第一...

    Java面试宝典2010版

    11、有数组a[n],用java代码将数组元素顺序颠倒 80 12.金额转换,阿拉伯数字的金额转换成中国传统的形式如:(¥1011)-&gt;(一千零一拾一元整)输出。 81 三. html&JavaScript&ajax部分 82 1. 判断第二个日期比第一...

    Java编程中关于异常处理的佳实践

    书写一个强健的代码更多的是一门艺术而不仅仅是一门科学,这里我们将讨论一些关于异常处理的Java佳实践。这些Java佳实践 甚至被标准JDK库,以及一些开源代码所追随,以更好的处理错误与异常。这也成为了Java程序员...

    java 面试题 总结

    7、说出ArrayList,Vector, LinkedList的存储性能和特性 ArrayList和Vector都是使用数组方式存储数据,此数组元素数大于实际存储的数据以便增加和插入元素,它们都允许直接按序号索引元素,但是插入元素要涉及数组...

    Java网络编程(第三版)中文版.part09.rar

    内容简介回到顶部↑《Java网络编程》第三版会为你介绍Java网络API的最新特性。本书讨论了JDK 1.4和1.5(现在已命名为J2SE 5)中所做的所有修改和增补。本书内容全面,涵盖了从网络基础知识到远程方法调用(RMI)等各...

    java面试题大全(2012版)

    11、有数组a[n],用java代码将数组元素顺序颠倒 80 12.金额转换,阿拉伯数字的金额转换成中国传统的形式如:(¥1011)-&gt;(一千零一拾一元整)输出。 81 三. html&JavaScript;&ajax;部分 82 1. 判断第二个日期比第...

    jdk1.8.0版本安装包

    增加新特性Lambda表达式的内部类改造,使得代码在书写上变得更加简洁 3、强大的 Stream API 增加了核心功能,使得代码调用方式变得更加简洁 4、便于并行 5、最大化减少空指针异常 Optional

    java面试题

    答:JDO是java对象持久化的新的规范,为java data object的简称,也是一个用于存取某种数据仓库中的对象的标准化API。 CORBA? 答:CORBA标准是公共对象请求代理结构,用途为:用不同的程序设计语言书写,在不同的...

    Java面试笔试资料大全

    11、有数组a[n],用java代码将数组元素顺序颠倒 80 12.金额转换,阿拉伯数字的金额转换成中国传统的形式如:(¥1011)-&gt;(一千零一拾一元整)输出。 81 三. html&JavaScript&ajax部分 82 1. 判断第二个日期比第一...

    java面试宝典2012

    11、有数组a[n],用java代码将数组元素顺序颠倒 87 12.金额转换,阿拉伯数字的金额转换成中国传统的形式如:(¥1011)-&gt;(一千零一拾一元整)输出。 88 三. html&JavaScript;&ajax;部分 89 1. 判断第二个日期比第...

    JAVA面试宝典2010

    11、有数组a[n],用java代码将数组元素顺序颠倒 80 12.金额转换,阿拉伯数字的金额转换成中国传统的形式如:(¥1011)-&gt;(一千零一拾一元整)输出。 81 三. html&JavaScript&ajax部分 82 1. 判断第二个日期比第一...

    Java面试宝典2012版

    11、有数组a[n],用java代码将数组元素顺序颠倒 80 12.金额转换,阿拉伯数字的金额转换成中国传统的形式如:(¥1011)-&gt;(一千零一拾一元整)输出。 81 三. html&JavaScript;&ajax;部分 82 1. 判断第二个日期...

Global site tag (gtag.js) - Google Analytics