JDK 1.7 新特性
1.switch中可以使用字串了
2."<>"这个玩意儿的运用List<String> tempList = new ArrayList<>(); 即泛型实例化类型自动推断。
mport java.util.*;
public class JDK7GenericTest {
public static void main(String[] args) {
// Pre-JDK 7
List<String> lst1 = new ArrayList<String>();
// JDK 7 supports limited type inference for generic instance creation
List<String> lst2 = new ArrayList<>();
lst1.add("Mon");
lst1.add("Tue");
lst2.add("Wed");
lst2.add("Thu");
for (String item: lst1) {
System.out.println(item);
}
for (String item: lst2) {
System.out.println(item);
}
}
}
3. 自定义自动关闭类 以下是jdk7 api中的接口,(不过注释太长,删掉了close()方法的一部分注释)
/** * A resource that must be closed when it is no longer needed. * * @author Josh Bloch * @since 1.7 */ public interface AutoCloseable { /** * Closes this resource, relinquishing any underlying resources. * This method is invoked automatically on objects managed by the * {@code try}-with-resources statement. * */ void close() throws Exception; } 只要实现该接口,在该类对象销毁时自动调用close方法,你可以在close方法关闭你想关闭的资源,例子如下 class TryClose implements AutoCloseable { @Override public void close() throw Exception { System.out.println(" Custom close method … close resources "); } } //请看jdk自带类BufferedReader如何实现close方法(当然还有很多类似类型的类) public void close() throws IOException { synchronized (lock) { if (in == null) return; in.close(); in = null; cb = null; } }
|
对Java集合(Collections)的增强支持 在JDK1.7之前的版本中,Java集合容器中存取元素的形式如下: 以List、Set、Map集合容器为例:
//创建List接口对象
//创建Set接口对象
//创建Map接口对象
在JDK1.7中,摒弃了Java集合接口的实现类,如:ArrayList、HashSet和HashMap。而是直接采用[]、{}的形式存入对象,采用[]的形式按照索引、键值来获取集合中的对象,如下: List<String> list=["item"]; //向List集合中添加元素 Set<String> set={"item"}; //向Set集合对象中添加元素 Map<String,Integer> map={"key":1}; //向Map集合中添加对象
3.数值可加下划线 例如:int one_million = 1_000_000; 4.支持二进制文字 例如:int binary = 0b1001_1001;
5.简化了可变参数方法的调用 当程序员试图使用一个不可具体化的可变参数并调用一个*varargs* (可变)方法时,编辑器会生成一个“非安全操作”的警告。
6、在try catch异常扑捉中,一个catch可以写多个异常类型,用"|"隔开, jdk7之前:
try { ...... } catch(ClassNotFoundException ex) { ex.printStackTrace(); } catch(SQLException ex) { ex.printStackTrace(); }
jdk7例子如下 try { ...... } catch(ClassNotFoundException|SQLException ex) { ex.printStackTrace(); }
7、jdk7之前,你必须用try{}finally{}在try()内使用资源,在finally中关闭资源,不管try中的代码是否正常退出或者异常退出。jdk7之后,你可以不必要写finally语句来关闭资源,只要你在try()的括号内部定义要使用的资源。 前提是try()内的那些资源要实现自定义自动关闭类,这是新特性 try-with-resources statement。
请看例子: jdk7之前
import java.io.*; // Copy from one file to another file character by character. // Pre-JDK 7 requires you to close the resources using a finally block. public class FileCopyPreJDK7 { public static void main(String[] args) { BufferedReader in = null; BufferedWriter out = null; try { in = new BufferedReader(new FileReader("in.txt")); 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(); } finally { // always close the streams try { if (in != null) in.close(); if (out != null) out.close(); } catch (IOException ex) { ex.printStackTrace(); } } try { in.read(); // Trigger IOException: Stream closed } catch (IOException ex) { ex.printStackTrace(); } } }
jdk7之后
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(); } } }
JAVA7新增加了Objects类 它提供了一些方法来操作对象,这些方法大多数是“空指针”安全的,比如 Objects.toString(obj);如果obj是null,,则输出是null。 否则如果你自己用obj.toString(),如果obj是null,则会抛出NullPointerException. |