`

今天看到关于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  

相关推荐

    代码书写器SciLexer

    **代码书写器SciLexer**是一款专门用于编写C/C++源代码的编辑器,它以其简洁、美观和功能强大的特点受到程序员的青睐。SciLexer是该编辑器的核心组件,它提供了丰富的语法高亮和代码提示功能,极大地提高了代码的...

    Java 8特性教程

    Java 8是Java语言发展的一个重要里程碑,它在Java编程领域引入了诸多创新特性,极大地方便了开发者的编程工作。接下来,我们将详细解读Java 8中的几个核心新特性,并通过实例加以说明。 1. 默认接口方法(Default ...

    java源代码编译java源代码编译

    Java源代码编译是将程序员用Java编程语言书写的源代码转换成计算机可以直接执行的机器码的过程。在Java中,这个过程分为两个主要步骤:编译和解释。本篇文章将详细探讨这两个步骤以及相关的工具、原理和技术。 1. *...

    嵌入式软件代码书写的十个技巧

    以下是一些关于编写更易维护嵌入式软件代码的十个技巧: 技巧1:避免使用汇编语言 虽然在低端的PIC微控制器上你可能没有选择,而在高端的ARM处理器上你可能不需要使用汇编语言,但是在两者之间存在大量的平台,它们...

    电子白板的Java源代码

    通过这种方式,我们可以方便地添加新的图形类型。 在电子白板的设计中,事件处理机制扮演着关键角色。Java提供了强大的事件监听和处理框架,比如AWT和Swing库中的事件模型。例如,当用户点击鼠标时,系统会触发一个...

    JAVA小工具V3.0源代码

    在Java编程中,源代码是程序的基础,它是由程序员用Java语言书写的文本文件,通常以.java为扩展名。这个项目提供的源代码,可以帮助用户理解一个简单的Java集成开发环境(IDE)是如何构建的,这包括代码编辑器、...

    JAVA小游戏 俄罗斯方块游戏源代码

    在Java编程领域,源代码是程序员用高级语言书写的程序文本,它能被编译器转换成计算机可执行的机器码。在这个项目中,开发者使用了Java的面向对象特性来实现游戏逻辑,如方块的生成、旋转、移动以及消除行等核心功能...

    Java开发环境_Jdk7_32位

    2. **try-with-resources**:这是Java 7引入的一个新特性,允许开发者在一个try语句块中声明资源,这些资源会在try语句块结束时自动关闭,无论是否抛出异常,从而避免了资源泄露的问题。 3. **钻石操作符**:在Java...

    Java for Programmers Second Edition示例代码

    8. **Java高级特性**:包括泛型、枚举、注解、Lambda表达式、流API等,这些特性使得Java代码更简洁、高效。示例将帮助读者理解如何在实际项目中应用这些特性。 通过阅读并实践《Java for Programmers Second ...

    java学习笔记源代码(第6版)-林信良

    《Java学习笔记源代码(第6版)-林信良》是针对初学者精心编写的Java编程学习资源,包含了丰富的示例代码,旨在帮助新手快速掌握Java编程基础。本资源的核心在于通过实践来驱动理论学习,使学习者能够在实际操作中...

    Java语言程序设计教材源代码

    这本书详细介绍了Java编程的基础知识和高级特性,旨在帮助读者掌握Java编程技术,理解面向对象编程的思想,并能够运用到实际项目中。 在Java语言的学习过程中,源代码是极其关键的部分。源代码是程序员用人类可读的...

    Java书写的俄罗斯方块

    Java的跨平台特性使得开发的游戏可以在多种操作系统上运行,只需一次编写,到处运行。此外,Java的性能稳定,内存管理优秀,这在实时性要求高的游戏开发中尤为重要。 俄罗斯方块是一款经典的休闲益智游戏,主要由...

    java8.pdf详细说明java8的使用说明

    这一部分将探讨Java 8中关于并发编程的新特性,包括新的执行器框架、同步机制、原子变量、ConcurrentMap等。 13. API示例(API Examples): 包括字符串、数值、算术和文件操作的API示例,这些示例展示了Java 8的新...

    java_jdk1.7x64

    - **类型推断 for varargs** 和 **钻石操作符**:编译器可以自动推断泛型的类型,简化代码书写。 2. **文件系统API增强**: - Java 7引入了NIO.2(New I/O 2),提供了一套新的文件系统API,包括Path、Files和...

    Java 8的新特性—终极版

    ### Java 8的新特性详解 #### 一、概述 Java 8作为自Java 5以来最重要的版本更新之一,带来了诸多革新之处。它不仅在语言层面引入了许多改进,还在编译器、库、工具以及JVM等方面实现了显著的增强。本文旨在全面地...

    java程序书写规范.doc

    以下是一些关键的Java程序书写规范: 1. **命名规范** - **标识符**:应使用有意义的英文描述符,避免使用缩写,除非在整个项目中有统一的缩写规则。名字长度应适中,一般不超过15个字母,以减少阅读难度。 - **...

    Java_API_1.7中文 高清完整CHM版

    这个高清完整的CHM版,提供了一个方便的离线查阅环境,对于学习和理解Java 7的特性和功能非常有帮助。 1. **核心API**:Java API 1.7的核心包括基础类库,如`java.lang`、`java.io`、`java.util`等。`java.lang`是...

    JAVA语言入门(CHM) JAVA语言入门(CHM)

    7. **可移植性**:Java代码可以轻松地从一个平台迁移到另一个平台,只需安装相应的JRE即可。 8. **动态性**:Java支持动态加载类库和代码重构,使程序更加灵活。 #### 四、Java开发环境搭建 1. **下载JDK**:从...

    JDK1.5新特性

    4. **静态导入(static import)**:静态导入允许开发者直接使用类中的静态成员,无需每次都指定类名,简化了代码书写。 5. **增强的for循环(foreach或for-each)**:增强的for循环提供了一种简洁的方式来遍历数组...

Global site tag (gtag.js) - Google Analytics