1,Binary Literals 二进制类型的支持
1-1 支持四种数据类型: byte, short, int, and long
前缀用 0b or 0B
// An 8-bit 'byte' value:
byte aByte = (byte)0b00100001;
// A 16-bit 'short' value:
short aShort = (short)0b1010000101000101;
// Some 32-bit 'int' values:
int anInt1 = 0b10100001010001011010000101000101;
int anInt2 = 0b101;
int anInt3 = 0B101; // The B can be upper or lower case.
// A 64-bit 'long' value. Note the "L" suffix:
long aLong = 0b1010000101000101101000010100010110100001010001011010000101000101L;
该四种数据类型的数组同样也是支持二进制类型的
public static final int[] phases = {
0b00110001,
0b01100010,
0b11000100,
0b10001001,
0b00010011,
0b00100110,
0b01001100,
0b10011000
}
1-2 You can use binary literals to make a bitmap more readable
使用二进制类型表示图片,使bitmap更具可读性
public static final short[] HAPPY_FACE = {
(short)0b0000011111100000;
(short)0b0000100000010000;
(short)0b0001000000001000;
(short)0b0010000000000100;
(short)0b0100000000000010;
(short)0b1000011001100001;
(short)0b1000011001100001;
(short)0b1000000000000001;
(short)0b1000000000000001;
(short)0b1001000000001001;
(short)0b1000100000010001;
(short)0b0100011111100010;
(short)0b0010000000000100;
(short)0b0001000000001000;
(short)0b0000100000010000;
(short)0b0000011111100000;
}
2,整数类型可以用_进行分割,方便阅读。
_ 仅仅用在两个数字之间。
不能用的地方:
At the beginning or end of a number
Adjacent to a decimal point in a floating point literal
Prior to an F or L suffix
In positions where a string of digits is expected
1,数字的开始或者结尾。
2,小数点两侧
3,字符F或者L前后
4,数字字符串不被使用 String age = "23"
float pi1 = 3_.1415F; // Invalid; cannot put underscores adjacent to a decimal point
float pi2 = 3._1415F; // Invalid; cannot put underscores adjacent to a decimal point
long socialSecurityNumber1
= 999_99_9999_L; // Invalid; cannot put underscores prior to an L suffix
int x1 = _52; // This is an identifier, not a numeric literal
int x2 = 5_2; // OK (decimal literal)
int x3 = 52_; // Invalid; cannot put underscores at the end of a literal
int x4 = 5_______2; // OK (decimal literal)
int x5 = 0_x52; // Invalid; cannot put underscores in the 0x radix prefix
int x6 = 0x_52; // Invalid; cannot put underscores at the beginning of a number
int x7 = 0x5_2; // OK (hexadecimal literal)
int x8 = 0x52_; // Invalid; cannot put underscores at the end of a number
int x9 = 0_52; // OK (octal literal)
int x10 = 05_2; // OK (octal literal)
int x11 = 052_; // Invalid; cannot put underscores at the end of a number
3, switch 语句支持String类型
public String getTypeOfDayWithSwitchStatement(String dayOfWeekArg) {
String typeOfDay;
switch (dayOfWeekArg) {
case "Monday":
typeOfDay = "Start of work week";
break;
case "Tuesday":
case "Wednesday":
case "Thursday":
typeOfDay = "Midweek";
break;
case "Friday":
typeOfDay = "End of work week";
break;
case "Saturday":
case "Sunday":
typeOfDay = "Weekend";
break;
default:
throw new IllegalArgumentException("Invalid day of the week: " + dayOfWeekArg);
}
return typeOfDay;
}
4,Type Inference for Generic Instance Creation 类型推断用于泛型实例创建
比如 java集合类:
Map<String, List<String>> myMap = new HashMap<String, List<String>>();
在jdk7中:
Map<String, List<String>> myMap = new HashMap<>();
List<String> list = new ArrayList<>();
list.add("A");
list.addAll(new ArrayList<>()); // 报错
list.addAll(list);
自定义泛型类
class MyClass<X> {
<T> MyClass(T t) {
// ...
}
}
创建实例:
MyClass<Integer> myObject = new MyClass<>("");
MyClass<String> myObject = new MyClass<>("");
5,Improved Compiler Warnings and Errors When Using Non-Reifiable Formal Parameters with Varargs Methods
编译器会提高警告和错误,在你使用非具体化参数(泛型)同时也是可变参数时。
比如:(T… elements)
该页包含几个专题:
1,堆污染,堆错误
2,可变参数和非具体化参数(泛型参数)
3,泛型可变参数潜在漏洞
4,当使用泛型可变参数提出安全警告
1,堆污染
List l = new ArrayList<Number>();
List<String> ls = l; // unchecked warning
l.add(0, new Integer(42)); // another unchecked warning
String s = ls.get(0); // ClassCastException is thrown
错误点:1,l赋值给ls。2,add方法。
2,可变参数和非具体化参数(泛型参数)
public class ArrayBuilder {
public static <T> void addToList (List<T> listArg, T... elements) {
for (T x : elements) {
listArg.add(x);
}
}
public static void faultyMethod(List<String>... l) {
Object[] objectArray = l; // Valid
objectArray[0] = Arrays.asList(new Integer(42));
String s = l[0].get(0); // ClassCastException thrown here
}
}
3,泛型可变参数潜在漏洞
public static void faultyMethod(List<String>... l) {
Object[] objectArray = l; //Valid
objectArray[0] = Arrays.asList(new Integer(42));
String s = l[0].get(0); // ClassCastException thrown here
}
4,当使用泛型可变参数提出安全警告,添加警告注解
@SafeVarargs
@SuppressWarnings(“unchecked”)
@SuppressWarnings(“varargs”) 这个使用编译参数:-Xlint:varargs
6,The try-with-resources Statement
主要就是资源的自动回收
语法: try(资源) 注意区别try{}catch{}
备注:该资源必须实现了AutoCloseable or Closeable 接口
例子:
在finally中关闭BufferedReader
static String readFirstLineFromFileWithFinallyBlock(String path) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(path));
try {
return br.readLine();
} finally {
if (br != null) br.close();
}
}
try-with-resources 语句:
Classes That Implement the AutoCloseable or Closeable Interface
static String readFirstLineFromFileWithFinallyBlock(String path) throws IOException {
try(BufferedReader br = new BufferedReader(new FileReader(path))) {
return br.readLine();
}
}
7,Catching Multiple Exception Types and Rethrowing Exceptions with Improved Type Checking
合并多异常和改良重抛异常检查
1,合并多异常
catch (IOException ex) {
logger.log(ex);
throw ex;
} catch (SQLException ex) {
logger.log(ex);
throw ex;
}
在JDK7中:
catch (IOException|SQLException ex) {
logger.log(ex);
throw ex;
}
2,重抛异常
static class FirstException extends Exception { }
static class SecondException extends Exception { }
public void rethrowException(String exceptionName) throws Exception {
try {
if (exceptionName.equals("First")) {
throw new FirstException();
} else {
throw new SecondException();
}
} catch (Exception e) {
throw e;
}
}
在JDK7中:
public void rethrowException(String exceptionName)
throws FirstException, SecondException {
try {
// ...
}
catch (Exception e) {
throw e;
}
}