1. JDK版本特性
JDK1.5新特性:
- 自动装箱与拆箱
- for-each
JDK 1.7 新特性
switch中可以使用字串了
String s = "test"; switch (s) { case "test" : System.out.println("test"); case "test1" : System.out.println("test1"); break ; default : System.out.println("break"); break ;
“<>”这个玩意儿的运用List tempList = new ArrayList<>(); 即泛型实例化类型自动推断。
自定义自动关闭类
public interface AutoCloseable { void close() throws Exception; }
java7开始,InputStream,OutputStream实现了自动关闭AutoClosable接口
jdk7之前,你必须用try{}finally{}在try内使用资源,在finally中关闭资源,不管try中的代码是否正常退出或者异常退出。jdk7之后,你可以不必要写finally语句来关闭资源,只要你在try()的括号内部定义要使用的资源。请看例子:
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(); } } }
在try catch异常扑捉中,一个catch可以写多个异常类型,用”|”隔开
jdk7之前:try { ...... } catch(ClassNotFoundException ex) { ex.printStackTrace(); } catch(SQLException ex) { ex.printStackTrace(); }
dk7例子如下
try { ...... } catch(ClassNotFoundException | SQLException ex) { ex.printStackTrace(); }
更多参考:
http://www.cnblogs.com/dassmeta/p/5755938.html
2. 多线程
2.1 场景题:三个线程同时start,用什么方法可以保证线程的执行顺序,线程1执行完线程2执行,线程2执行完线程3执行?
分析1(不可行):已经start( )了的多个线程,再调用join( ),无效。因为start后已经开始抢占式了。如下无效:
thread1.start(); thread2.start(); thread3.start(); thread1.join(); //无效 thread2.join(); //无效 thread3.join(); //无效
分析2(不符合要求):start一个线程后调用join,然后再start其他线程。可行,但本题要求同时启动,略微跑题。
thread1.start(); thread1.join(); thread2.start(); thread2.join(); thread3.start(); thread3.join();
分析3(符合):在线程中的关键方法run中添加代码,在一个线程的target中用join()方法确保其他线程执行完。如下:
public static void main(String[] args) { final Thread t1=new Thread(new Runnable(){ @Override public void run() { System.out.println("t1"); } }); final Thread t2 = new Thread(new Runnable() { @Override public void run() { try{ t1.join(); }catch(InterruptedException e){ } System.out.println("t2"); } }); final Thread t3=new Thread(new Runnable(){ @Override public void run() { try { t2.join(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("t3"); } }); t1.start(); t2.start(); t3.start(); }
执行的结果一定为:
t1
t2
t3分析4(CountDownLatch,可行)。为线程类传入上一个线程的countDwon,和本线程的countDown。在run中执行业务前,先上一个线程计数器.await( ),然后执行业务,之后本线程计数器.countDown( )。
详情见本人博客:
http://blog.csdn.net/answer100answer/article/details/77658097
3.集合
3.1 hashmap的原理
java7采用数组+链表
java8采用数组+链表+红黑树的结构(当链表长度大于8时转红黑树)。