`
david.org
  • 浏览: 157429 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

New language features in Java 7

阅读更多
Here are 7 of the new features that have been completed:
• Language support for collections
• Automatic Resource Management
• Improved Type Inference for Generic Instance Creation (diamond)
• Underscores in numeric literals
• Strings in switch
• Binary literals
• Simplified Varargs Method Invocation

There is a lot more to Java 7 then just these language changes. I’ll be exploring the rest of the release in future posts. One of the big debates is currently around Closures, which are a separate JSR.

Language support for collections
Java will be getting first class language support for creating collections. The style change means that collections can be created like they are in Ruby, Perl etc.
Instead of:
List<String> list = new ArrayList<String>();
list.add("item");
String item = list.get(0);

Set<String> set = new HashSet<String>();
set.add("item");

Map<String, Integer> map = new HashMap<String, Integer>();
map.put("key", 1);
int value = map.get("key");

You will be able to use:
List<String> list = ["item"];
String item = list[0];

Set<String> set = {"item"};

Map<String, Integer> map = {"key" : 1};
int value = map["key"];

These collections are immutable.

Automatic Resource Management
Some resources in Java need to be closed manually like InputStream, Writers, Sockets, Sql classes. This language feature allows the try statement itself to declare one of more resources. These resources are scoped to the try block and are closed automatically.
This:
BufferedReader br = new BufferedReader(new FileReader(path));
try {
   return br.readLine();
} finally {
   br.close();
}

becomes:
try (BufferedReader br = new BufferedReader(new FileReader(path)) {
   return br.readLine();
}
You can declare more than one resource to close:
try (
   InputStream in = new FileInputStream(src);
   OutputStream out = new FileOutputStream(dest))
{
 // code
}

To support this behaviour all closable classes will be retro-fitted to implement a Closable interface.

Improved Type Inference for Generic Instance Creation (diamond)
This is a particular annoyance which is best served with an example:
Map<String, List<String>> anagrams = new HashMap<String, List<String>>();

becomes:
Map<String, List<String>> anagrams = new HashMap<>();

This is called the diamond operator: <> which infers the type from the reference declaration.

Underscores in numeric literals

Long numbers are hard to read. You can now split them up using an underscore in ints and longs:
int one_million = 1_000_000;


Strings in switch
Currently you can only use numbers or enums in switch statements. String has been added as a candidate:
String s = ...
switch(s) {
 case "quux":
    processQuux(s);
    // fall-through

  case "foo":
  case "bar":
    processFooOrBar(s);
    break;

  case "baz":
     processBaz(s);
    // fall-through

  default:
    processDefault(s);
    break;
}


Binary literals
Java code, due to its C heritage, has traditionally forced programmers to represent numbers in only decimal, octal, or hexadecimal.
As quite a few domains are bit orientated, this restriction can introduce errors. You can now create binary numbers using an 0b prefix.
int binary = 0b1001_1001;


Simplified Varargs Method Invocation
When a programmer tries to invoke a *varargs* (variable arity) method with a non-reifiable varargs type, the compiler currently generates an “unsafe operation” warning. JDK 7 moves the warning from the call site to the method declaration. This will enable API designers to use varargs due to the reduction of warnings reported.

原文地址: http://code.joejag.com/2009/new-language-features-in-java-7/
分享到:
评论

相关推荐

    Modern Java In Action 2019

    Put simply, the new features in Java 8 along with the (less-obvious) changes in Java 9 are the biggest change to Java in the 21 years since Java 1.0 was released. Nothing has been taken away, so all ...

    Java in easy steps: Covers Java 9, 6th Edition - epub格式

    This 6th edition of Java in easy steps covers the new features of Java 9, including: REPL (a Read-Eval-Print-Loop) – a new feature of Java 9. This is an interactive shell named JShell, which will be...

    Java Closures and Lambda(Apress,2015)

    These new changes make their debut in Java 8, and their highlight is the long-awaited support for lambda expressions in the Java language. You'll learn to write lambda expressions and use them to ...

    Java Language Specification, Third Edition

    It provides full coverage of all new features added in since the previous edition including generics, annotations, asserts, autoboxing, enums, for each loops, variable arity methods and static import...

    Data Structures and Algorithms in Java, 5th Edition (Part 3/3)

    Updates to Java 5.0 include new sections on generics and other Java 5.0 features, and revised code fragments, examples, and case studies to conform to Java 5.0. Hundreds of exercises, including many...

    Mastering+Java+9-Packt+Publishing(2017).pdf

    Java 9 and its new features add to the richness of the language--one of the most-used languages to build robust software applications. Java 9 comes with a special emphasis on modularity, implemented ...

    Android代码-java8-tutorial

    &gt; You should also read my Java 11 Tutorial (including new language and API features from Java 9, 10 and 11). Welcome to my introduction to Java 8. This tutorial guides you step by step through all new...

    Java语言规范

    THE Java™ programming language is a general-purpose, concurrent, classbased, object-oriented language. It is designed to be simple enough that many programmers can achieve ...new and untested features.

    java7帮助文档

    Documentation is regularly updated to provide developers with in-depth information about new features in the Java platform. Some recent updates include: Swing The JLayer class has been added, which ...

    Data Structures and Algorithms in Java, 5th Edition (Part 2/3)

    Updates to Java 5.0 include new sections on generics and other Java 5.0 features, and revised code fragments, examples, and case studies to conform to Java 5.0. Hundreds of exercises, including many...

    Addison.Wesley.The.Java.Programming.Language.4th.Edition.Aug.2005.chm

    There are new chapters on generics, enums, and annotationsthe major new language features introduced in the 5.0 releaseand major new sections on assertions and regular expressions. Some existing ...

    Mastering Java 9

    You'll be provided with an overview and explanation of the new features introduced in Java 9 and the importance of the new APIs and enhancements. Some of the new features of Java 9 are ground-breaking...

    Core Java Volume II Advanced Features__10th edition

    Now, Core Java®, Volume II—Advanced Features, Tenth Edition, has been extensively updated to reflect the most eagerly awaited and innovative version of Java in years: Java SE 8. Rewritten and ...

    Core Java(Volume II--Advanced Features 9th Edition)

    Fully updated to reflect Java SE 7 language changes, Core Java®, Volume II—Advanced Features, Ninth Edition, is the definitive guide to Java’s most powerful features for enterprise and desktop ...

    Core.Java.B013WZRDNQ

    The course will use JDK(Java Development Kit) and covers all the new language features. This course includes worked out examples illustrating new JDK features that are fully explained throughout the ...

    Learning Java

    If you're new to Java, the fourth edition of this bestselling guide provides an example-driven introduction to the latest language features and APIs in Java 6 and 7. Advanced Java developers will be ...

    Java 9_Building Robust Modular Applications-Packt Publishing(2018)

    Java 9 and its new features add to the richness of the language--one of the most-used languages to build robust software applications. Java 9 comes with a special emphasis on modularity. Some of the ...

Global site tag (gtag.js) - Google Analytics