java 7
强引用、弱引用、软引用、虚引用
- 强引用
Object o=new Object();
回收 o=null; - 软引用
SoftReference
softRef=new SoftReference (new String("abc"));
回收 如果一个对象只具有软引用,且内存空间不足,就会回收 - 弱引用
WeakReference
abcWeakRef = new WeakReference (new String("abc"));
回收 gc扫描发现只具有弱引用的对象,立即回收 - 虚引用
回收
类型推导 操作符<>
//JDK 7之前
Map<String, List<String>> employeeRecords = new HashMap<String, List<String>>();
List<Integer> primes = new ArrayList<Integer>();
//JDK 7
Map<String, List<String>> employeeRecords = new HashMap<>();
List<Integer> primes = new ArrayList<>();
在switch中支持String 大小写敏感
String state = "NEW";
switch (day) {
case "NEW": System.out.println("Order is in NEW state"); break;
case "CANCELED": System.out.println("Order is Cancelled"); break;
case "REPLACE": System.out.println("Order is replaced successfully"); break;
case "FILLED": System.out.println("Order is filled"); break;
default: System.out.println("Invalid");
}
自动资源管理 try-with-resource
public static void main(String args[]) {
try (
FileInputStream fin = new FileInputStream("info.xml");
BufferedReader br = new BufferedReader(new InputStreamReader(fin));
)
{
if (br.ready()) {
String line1 = br.readLine();
System.out.println(line1);
}
} catch (FileNotFoundException ex) {
System.out.println("Info.xml is not found");
} catch (IOException ex) {
System.out.println("Can't read the file");
}
}
Fork Join框架
数值字面量中使用下划线
int billion = 1_000_000_000; // 10^9
long creditCardNumber = 1234_4567_8901_2345L; //16 digit number
long ssn = 777_99_8888L;
double pi = 3.1415_9265;
float pif = 3.14_15_92_65f;
//不能在小数后面,或者数字的开始和结束的地方放下划线
double pi = 3._1415_9265; // underscore just after decimal point
long creditcardNum = 1234_4567_8901_2345_L; //underscore at the end of number
long ssn = _777_99_8888L; //undersocre at the beginning
在一个catch块中捕获多个异常
try {
......
} catch(ClassNotFoundException|SQLException ex) {
ex.printStackTrace();
}
//不包括异常的子类型的
//error
try {
......
} catch (FileNotFoundException | IOException ex) {
ex.printStackTrace();
}