1.switch中可以使用字串;

2."<>"这个玩意儿的运用List<String> tempList = new ArrayList<>(); 即泛型实例化类型自动推断;

3.自定义自动关闭类AutoCloseable

4.新增一些取环境信息的工具方法

File System.getJavaIoTempDir() // IO临时文件夹
File System.getJavaHomeDir() // JRE的安装目录
File System.getUserHomeDir() // 当前用户目录
File System.getUserDir() // 启动java进程时所在的目录

5.Boolean类型反转,空指针安全,参与位运算

6.两个char间的equals

boolean Character.equalsIgnoreCase(char ch1, char ch2)

7.对Java集合(Collections)的增强支持

在JDK1.7之前的版本中,Java集合容器中存取元素的形式如下,以List、Set、Map集合容器为例:

//创建List接口对象
List<String> list=new ArrayList<String>();
list.add("item"); //用add()方法获取对象
String Item=list.get(0); //用get()方法获取对象

//创建Set接口对象
Set<String> set=new HashSet<String>();
set.add("item"); //用add()方法添加对象

//创建Map接口对象
Map<String,Integer> map=new HashMap<String,Integer>();
map.put("key",1); //用put()方法添加对象
int value=map.get("key");

在JDK1.7中,摒弃了Java集合接口的实现类,如:ArrayList、HashSet和HashMap。而是直接采用[]、{}的形式存入对象,采用[]的形式按照索引、键值来获取集合中的对象,如下:

List<String> list=["item"]; //向List集合中添加元素
String item=list[0]; //从List集合中获取元素

Set<String> set={"item"}; //向Set集合对象中添加元素
Map<String,Integer> map={"key":1}; //向Map集合中添加对象
int value=map["key"]; //从Map集合中获取对象

8.数值可加下划线

例如:int one_million = 1_000_000;

9.支持二进制文字

例如:int binary = 0b1001_1001;

10.简化了可变参数方法的调用

当程序员试图使用一个不可具体化的可变参数并调用一个*varargs* (可变)方法时,编辑器会生成一个“非安全操作”的警告。

11.在try catch异常扑捉中,一个catch可以写多个异常类型,用"|"隔开

try {
   ......
} catch(ClassNotFoundException|SQLException ex) {
   ex.printStackTrace();
}

12.jdk7之前,你必须用try{}finally{}在try内使用资源,在finally中关闭资源,不管try中的代码是否正常退出或者异常退出。jdk7之后,你可以不必要写finally语句来关闭资源,只要你在try()的括号内部定义要使用的资源

import java.io.*;
// Copy from one file to another file character by character.
// JDK 7 has a try-with-resources statement, which ensures that
// each resource opened in try() is closed at the end of the statement.
public class FileCopyJDK7 {
   public static void main(String[] args) {
      try (BufferedReader in  = new BufferedReader(new FileReader("in.txt"));
           BufferedWriter out = new BufferedWriter(new FileWriter("out.txt"))) {
         int charRead;
         while ((charRead = in.read()) != -1) {
            System.out.printf("%c ", (char)charRead);
            out.write(charRead);
         }
      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }
}

13.支持泛型菱形语法

Map<String,Object> map = new HashMap<>();

results matching ""

    No results matching ""