首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

Java 七 的 7 个新的 “酷” 特性

2012-10-28 
Java 7 的 7 个新的 “酷” 特性Java 7 即将在本月底发布了,先来看 Java 7 中的一些不错的新酷特性吧:1. swi

Java 7 的 7 个新的 “酷” 特性
Java 7 即将在本月底发布了,先来看 Java 7 中的一些不错的新酷特性吧:

1. switch 中使用字符串变量

public void testStringSwitch(String direction) {  switch (direction) {     case "up": y--;     break;     case "down": y++;     break;     case "left": x--;     break;     case "right": x++;     break;    default:System.out.println("Invalid direction!");    break;  }}


2. 简化泛型对象创建
// Java 7 以前版本Map<String,Map<String,int>>m=new HashMap<String, Map<String,int>>();// Java 7Map<String, Map<String, int>> m = new HashMap<>();


3. 多异常处理
try {    Class a = Class.forName("wrongClassName");    Object instance = a.newInstance();} catch (ClassNotFoundException | IllegalAccessException |   InstantiationException ex) {   System.out.println("Failed to create instance");}


4. 资源的自动释放
try (BufferedReader in=new BufferedReader(new FileReader("test.txt"))){    String line = null;    while ((line = in.readLine()) != null) {System.out.println(line);    }} catch (IOException ex) {    ex.printStackTrace();}


5. 文件 IO API 的改进
下面是列出一个文件中所有行并打印的方法
List<String> lines =  Files.readAllLines(FileSystems.getDefault().getPath("test.txt"), StandardCharsets.UTF_8);for (String line : lines) System.out.println(line);


6. 通过 invokedynamic支持非 Java 语言详细

7. JLayerPane详细


转载:http://www.oschina.net/question/12_23560

热点排行