java se7 变化_JavaSE7语言新特性记录

虽说做JAVA开发也有几年了,工作的原因所开发的系统都是在java6下开发的,最近看了下kotlin的语法,语法里有些确实让我眼前一亮的东西,在之前用java6开发中处理起来麻烦的在kotlin里有较好的解决,比如说空安全,方法、属性的覆盖,Lambda 表达式语法,if是表达式而在java里是语句,表达式就能带来表达式的好处,不胜枚举。因此让我对java6之后的版本新特性有了探一探的想法。

整数类型的二进制表式

在JavaSE7的整数类型(byte,short,int和long),也可以使用二进制数字系统表示。要指定二进制字面值,数值添加前缀0b或0B。16进制的数值表示增加0x

例如:

// 一个8位的'byte'类型值:

byte aByte = (byte)0b00100001;

// 一个16位的'short'类型值:

short aShort = (short)0b1010000101000101;

// 一个32位的'int'类型值:

int anInt1 = 0b10100001010001011010000101000101;

// 一个64位的'long'类型值. 长整型以L结尾:

long aLong = 0b1010000101000101101000010100010110100001010001011010000101000101L;

数字字面值中加下划线

你可以使用下划线(_)使数字常量更易读,下面的示例显示有效的在数字字面值中加下划线:

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;

下面这些不能在数字字面值的下列地方放置下划线的点都好理解:

在数字的开头或结尾与浮点数字中的小数点相邻F或L后缀之前0x7fff中0_x7fff这样加是不可以的

在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;

}

推断泛型类型参数

只要编译器可以从上下文中推断出类型参数,你就可以用一对空着的尖括号<>来代替泛型参数。只有构造器的参数化类型在上下文中被显著的声明了,你才可以使用类型推断,否则不行。

例如:

Map> myMap = new HashMap<>();

Map> myMap = new HashMap();// 编译警告

try-with-resources语句

try-with-resources 是一个定义了一个或多个资源的try 声明.这个资源是程序处理完它之后需要关闭的对象。try-with-resources 确保每一个资源在处理完成后都会被关闭。

任何实现了java.lang.AutoCloseable接口和java.io.Closeable接口的对象可以使用try-with-resources的资源。

java.io.InputStream, OutputStream, Reader, Writer, java.sql.Connection, Statement, and ResultSet这些类实现了AutoCloseable接口能够被用在try-with-resources语句中.

例如:

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);

// Open zip file and create output file with try-with-resources statement

try (

java.util.zip.ZipFile zf = new java.util.zip.ZipFile(zipFileName);

java.io.BufferedWriter writer = java.nio.file.Files.newBufferedWriter(outputFilePath, charset)

) {

// Enumerate each entry

for (java.util.Enumeration entries = zf.entries(); entries.hasMoreElements();) {

// Get the entry name and write it to the output file

String newLine = System.getProperty("line.separator");

String zipEntryName = ((java.util.zip.ZipEntry)entries.nextElement()).getName() + newLine;

writer.write(zipEntryName, 0, zipEntryName.length());

}

}

}

JAVA7之前关闭资源是在finally中,目的就是看开发有没有对申请使用的资源要关闭的意识。

捕获多个异常及增加类型检查后的重抛

在JavaSE7里,一个catch可以捕获多个异常,每个异常之间用 | 隔开。

......

catch (IOException|SQLException ex) {

logger.log(ex);

throw ex;

}

JavaSE7可以更准确的分析出你抛出的异常类型,可以在throws上抛出更加具体的异常类型,这个在JavaSE7是不可以的。

public void rethrowException(String exceptionName) throws FirstException, SecondException {

try {

// ...

} catch (Exception e) {

throw e;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值