二进制字面量
整数类型例如(byte,short,int,long)能够用二进制来表示了。通过在数字前面加入0b或者0B来标示一个二进制的字面量
//一个8位'byte'值:
byte aByte = (byte)0b00100001;
//一个16位'short'值:
short aShort = (short)0b1010000101000101;
//一些32位'int'值:
int anInt1 = 0b10100001010001011010000101000101;
int anInt2 = 0b101;
int anInt3 = 0B101; // B可以是大写也可以是小写.
//一个64位的'long'值. 注意"L"结尾:
long aLong = 0b1010000101000101101000010100010110100001010001011010000101000101L;
参考:https://blog.csdn.net/heartroll/article/details/78455045
在数字字面量使用下划线
这个功能可以用来对一个数字字面量根据位数分组,从而提高你代码的可读性。比如,如果你的代码包含一些数字有很多的位数,你能够用下划线字符把位数分为三组,类似于你用一个像逗号或者一个空格作为分隔符。
下划线只能出现在数字之间,下面的情形不能出现下划线:
- 数字的开头和结尾
- 在浮点数中与小数点相邻
- F或者L后缀之前
- 在预期数字串的位置
long creditCardNumber = 1234_5678_9012_3456L;
long socialSecurityNumber = 999_99_9999L;
float pi = 3.14_15F;
long hexBytes = 0xFF_EC_DE_5E;
long hexWords = 0xCAFE_BABE;
long maxLong = 0x7fff_ffff_ffff_ffffL;
byte nybbles = 0b0010_0101;
long bytes = 0b11010010_01101001_10010100_10010010;
float pi1 = 3_.1415F; // 无效; 不能和小数点相邻
float pi2 = 3._1415F; // 无效; 不能和小数点相邻
long socialSecurityNumber1 = 999_99_9999_L; // 无效; 不能放在L后缀之前
int x1 = _52; // 无效;这是个标识符,不是数字的字面量
int x2 = 5_2; // OK
int x3 = 52_; // 无效; 不能放在数字的结尾
int x4 = 5_______2; // OK
int x5 = 0_x52; // 无效; 不能放在 0x 中间
int x6 = 0x_52; // 无效; 不能放在数字的开头
int x7 = 0x5_2; // OK
int x8 = 0x52_; // 无效; 不能放在数字的结尾
int x9 = 0_52; // OK
int x10 = 05_2; // OK
int x11 = 052_; // Invalid; 不能放在数字的结尾
参考:https://blog.csdn.net/heartroll/article/details/78455045
Switch中引入String比较类型
在JDK1.5之前,switch循环只支持byte、short、char、int四种数据类型.
JDK7以前在switch 只支持
- 基本数据类型:byte, short, char, int
- 包装数据类型:Byte, Short, Character, Integer
- 枚举类型:Enum
在JDK7 开始支持字符串类型:String
switch语句比较与每个case标签关联就好像使用string.equals方法表达的表达式的字符串对象;因此,在switch语句的字符串对象的比较是区分大小写的。java编译器生成更有效的字节码从switch语句中使用字符串对象比链式if-then-else语句。
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;
}
参考:https://blog.csdn.net/heartroll/article/details/78455045
泛型推导
Map<String, List<String>> myMap = new HashMap<String, List<String>>();
//JDK7可以只在申明指定泛型
Map<String, List<String>> myMap = new HashMap<>();
参考:https://blog.csdn.net/heartroll/article/details/78455045
try-with-resources 资源的自动管理
- 自动关闭资源
try-with-resources语句是一个声明一个或多个资源的try语句。一个资源作为一个对象,必须在程序结束之后关闭。try-with-resources语句确保在语句的最后每个资源都被关闭,任何实现了java.lang.AutoCloseable和java.io.Closeable的对象都可以使用try-with-resource来实现异常处理和关闭资源。
//JDK1.7之前我们必须在finally块中手动关闭资源,否则会导致资源的泄露
public class PreJDK7 {
public static String readFirstLingFromFile(String path) throws IOException {
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(path));
return br.readLine();
} catch (IOException e) {
e.printStackTrace();
} finally {//必须在这里关闭资源
if (br != null)
br.close();
}
return null;
}
}
//JDK1.7之后就可以使用try-with-resources,不需要
public class AboveJDK7 {
static String readFirstLineFromFile(String path) throws IOException {
try (BufferedReader br = new BufferedReader(new FileReader(path))) {
return br.readLine();
}
}
}
- 异常抛出顺序
在JDK1.7之前如果rd.readLine()与rd.close()都抛出异常则只会抛出finally块中的异常,不会抛出rd.readLine()中的异常,这样经常会导致得到的异常信息不是调用程序想要得到的。
在JDK1.7及以后采用了try-with-resource机制,如果在try-with-resource声明中抛出异常(如文件无法打开或无法关闭)的同时rd.readLine()也抛出异常,则只会抛出rd.readLine()的异常。
- try-with-resource可以声明多个资源
public class AboveJDK7_2 {
public static void writeToFileZipFileContents(String zipFileName,String outputFileName) throws java.io.IOException {
java.nio.charset.Charset charset = java.nio.charset.Charset.forName("US-ASCII");
java.nio.file.Path outputFilePath = java.nio.file.Paths.get(outputFileName);
//打开zip文件,创建输出流
try (
java.util.zip.ZipFile zf = new java.util.zip.ZipFile(zipFileName);
java.io.BufferedWriter writer = java.nio.file.Files.newBufferedWriter(outputFilePath, charset)
){
//遍历文件写入txt
for (java.util.Enumeration entries = zf.entries(); entries.hasMoreElements();) {
String newLine = System.getProperty("line.separator");
String zipEntryName = ((java.util.zip.ZipEntry) entries.nextElement()).getName() + newLine;
writer.write(zipEntryName, 0, zipEntryName.length());
}
}
}
}
- catch 多个exception
//befor JDK7
try{
//逻辑代码
}catch (IOException ex) {
logger.log(ex);
throw new SpecialException();
catch (SQLException ex) {
logger.log(ex);
throw new SpecialException();
}
//after JDK7
try{
//逻辑代码
}catch (IOException | SQLException ex) {
logger.log(ex);
throw new SpecialException();
}
- Rethrowing Exception with more inclusive Type Checking
static class FirstException extends Exception {
}
static class SecondException extends Exception {
}
//before JDK7
public void rethrowException(String exceptionName) throws Exception {
try {
if (exceptionName.equals("First")) {
//如果异常名称为"First",则抛出异常一
throw new FirstException();
} else {
//否则的话,则抛出异常二
throw new SecondException();
}
} catch (Exception e) {
throw e;
}
}
//after JDK7
public void rethrowException(String exceptionName) throws FirstException, SecondException {
// 逻辑代码同上
}
try块中能抛出两种异常。在java SE7以前的版本中,在方法声明中throws 只能写Exception,因为catch里的类型是 Exception。 但是在java SE7及以后的版本中,可以在throws后面写 FirstException和SecondException——编译器能判断出throw e语句抛出的异常e 一定来自try块,并且try块只能抛出FirstException和SecondException。
参考:https://blog.csdn.net/heartroll/article/details/78455045