JAVA7 语法新特性

color=red]1、在switch语句中可以使用字符串类型[/color]
请看下例

public class SwitchSamlpe {

public static void main(String[] args){
SwitchSamlpe.show("man");
SwitchSamlpe.show("\u006d\u0061\u006e");
}

public static void show(String gender){
switch(gender){
case "man":
System.out.println("hi guy");
break;
case "woman":
System.out.println("hi beauty");
break;
default:
System.out.println("hi ");
}
}

}

运行以后会同时打出2个hi guy 可以看出直接用unicode编码会造成很多误会。而这个实现的核心也是把string转化成了对应的哈希值而已。
[color=red]2、二进制添加新的表述方式[/color]

public class LiteralSamlpe {
public static void main(String[] args){
System.out.println(0b10011);
System.out.println(0B10011);
}
}

增加OB以及Ob来表示2进制
[color=red]3、数值下面可以使用下划线来表示[/color]

public class LiteralSamlpe {
public static void main(String[] args){
System.out.println(150_000_000);
}
}

为了好区分增加了下划线来表示以上依然输出150000000

4、java7对异常的处理
先看以下例子

public class ExceptionSample {
@SuppressWarnings("finally")
public static void main(String[] args){
try{
throw new IOException("main exception");
}catch (Exception e) {
throw new RuntimeException(e.getMessage());
}finally{
try {
throw new IOException("sub exception");
} catch (IOException e) {
throw new RuntimeException(e.getMessage());
}
}
}

}

运行以后只能显示sub exception 这一个异常。那是因为在try中的主要异常被后面的finally中的sub异常所覆盖,这样会导致程序员判断出错
[color=red]java7提供了 addSuppressed 方法来解决这个问题[/color]

public class ExceptionSampleJava7 {
public static void main(String[] args) {
RuntimeException e1 = null;
try{
throw new IOException("main exception");
}catch (Exception e) {
e1= new RuntimeException(e.getMessage());
throw e1;
}finally{
try {
throw new IOException("sub exception");
} catch (final IOException e) {
e1.addSuppressed(e);
}
}
}
}

[color=red]Exception in thread "main" java.lang.RuntimeException: main exception
at com.xxx.java7.ExceptionSampleJava7.main(ExceptionSampleJava7.java:11)
Suppressed: java.io.IOException: sub exception
at com.xxx.java7.ExceptionSampleJava7.main(ExceptionSampleJava7.java:15)[/color]
运行以上代码就可以发现不但main异常被没被覆盖 同时也很清楚的记录着被suppressed的异常。
[color=red]5、java7 允许一个catch捕获多个异常[/color]

public class ExceptionSampleJava7 {
public static void main(String[] args) {
try {
throw new IOException("exception");
} catch (IOException | RuntimeException e) {
e.getStackTrace();
}
}
}


这样可以大大减少我们代码的复杂度。但是注意在前面的异常必须小于后面的异常。

[color=red]6、增加try-with-resources[/color]传统的IO操作我们都可以对流进行关闭
如下

public class TryWithSample {
@SuppressWarnings("unused")
private static void read(File source) {
InputStream fis = null;
try {
fis = new FileInputStream(source);
}
catch (Exception e) {
e.printStackTrace();
} finally {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}


这样在代码上面十分不美观 而且增加复杂度。
java7 为我们提供这样的书写方式

public class TryWithSample {
@SuppressWarnings("unused")
private static void read(File source) throws IOException {
try(InputStream fis = new FileInputStream(source)) {

}

}
}


这样JAVA会自动关闭在try中申请的管道。。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值