最近做项目用的Java7而自己却纠结于java5的特性,不能充分利用java7特性提供代码质量与开发效率,学习过程中还是要多做笔记,不然真的忘得特别快.最近学习了java7的特性,跟大家分享下学习笔记,也促使自己坚持下去,由于本人水平有限,希望大家看到不足之处,敬请指正.
1. switch 语句的增强:
Java7之前switch语句语法:
int i = 5;
switch (i){ case 1: System.out.println("I am 1"); break; case 5: System.out.println("I am 5"); break; default: System.out.println("I am not 1 and not 2"); }
系统输出 I am 5
其中i的类型仅能为(byte,shrot,int,char)四种类型case语句不能重复
switch语句Java7语法:
String i = "fight"; switch (i){ case "fight" : System.out.println("yes you should fight"); break; case "nofight": System.out.println("are you sure not fight"); break; default: System.out.println("I don't know"); }系统输出:yes you should fight支持 i 为字符串类型 case语句不能重复其实Java7支持switch语法是编译器的一个优化 ,将switch中的i和case语句的值转换成对应的hash值,这样Java虚拟机看到的还是与整型类型兼容的类型当为字符串类型的时候除了判断hash值还有利用String的equal方法进行比较,避免产生冲突,但当switch中case语句较多的时候推荐使用枚举类型增加可维护性2.字面量的改进(二进制字面量):通过在数字前面添加0b或者0BSystem.out.println(0b111);系统输出:73 在数字中间加_便于识别:System.out.println(1_1_1);系统输出:111注意下划线只能出现在数字中间4:异常的优化异常重大优化:1支持在一个catch语句中捕获多个异常,2 捕获之后重新抛出的异常更加精确异常分为两种:受捡异常checked 父类(Exception)与 非受捡异常(unchecked)父类(RuntmeException)两大类,两者的顶级父类(Throwable)对异常常用关键字的解析: throws:声明一个方法抛出的异常;throw:抛出一个异常:try{}catch(Exception e){}finally{}捕获一个异常异常丢失 try{}catch(){}finally{}更加精准的异常抛出:其中ExceptionA是 ExceptionSub1 ExceptionSub2 的父类,
try { throw new ExceptionSub1(); } catch (ExceptionA exceptionA) { try { throw exceptionA; ---编译器报错 因为try中抛出的ExceptionSub1异常所以catch中捕获的应该是ExceptionSub1 异常,而第一--catch中抛出的
exceptionA应该也是ExceptionSub1 而 第二个catch中捕获的ExceptionSub2 与 抛出的不 --致所以编译器报错
} catch (ExceptionSub2 exceptionSub2) { exceptionSub2.printStackTrace(); } }}异常丢失情况:我们关心的是try中抛出的异常,而当finally中也有异常抛出的时候会覆盖try中的异常,这是常见的异常丢失情况public void test(){ try{ throw new IOException("try异常"); }catch(IOException e){ }finally { try { throw new IOException("finally异常"); } catch (IOException e) { e.printStackTrace(); } } }通过这种方式抛出try中异常IOException read = null; try{ throw new IOException("try异常"); }catch(IOException e){ read = e; }finally { try { throw new IOException("finally异常"); } catch (IOException e) { if(read == null){ read = e; } } throw read; } }try{ throw new ExceptionA(); }catch (ExceptionA| RuntimeException e){ }java7异常优化:抛出全部异常IOException read = null; try{ throw new IOException("try异常"); }catch(IOException e){ read = e; }finally { try { throw new IOException("finally异常"); } catch (IOException e) { if(read != null){ read.addSuppressed(e); } } throw read; } }5:try -with-resource语句java7之前必须手动释放资源InputStream is = null; OutputStream out=null; try { is = new FileInputStream(new File("d://123.txt")); out = new FileOutputStream(new File("d://456.txt")); byte[] bytes = new byte[1024]; int length = 0; while ((length = is.read(bytes)) != -1) { out.write(bytes, 0, length); } }catch(IOException e){ }finally { if(is!=null){ try { is.close(); } catch (IOException e) { e.printStackTrace(); } } if(out!=null){ try { out.close(); } catch (IOException e) { e.printStackTrace(); } } }Java7优化不需要手动释放资源,但必须实现了AutoCloseable接口,方可public void test(String path) throws IOException { try( InputStream is = new FileInputStream(new File(path));OutputStream out = new FileOutputStream(new File(path))){ int length = 0; byte [] buffer = new byte[1024]; while ((length=is.read(buffer))!=-1){ out.write(buffer,0,length); } } }