Java7 编程语言特性

来源:互联网 发布:html windows.onload 编辑:程序博客网 时间:2024/05/16 17:28

jdk语言新特性:

1. 二进制表达方式:

/*  jdk1.6*/byte b = (byte) 2 ;short aShort = (short) 2 ;int anInt2 = 2;long aLong = 2;/*jdk1.7*/byte b = (byte)0b10 ;short aShort = (short) 0b10 ;int anInt2 = 0b10 ;long aLong = 0b10L;
2.新增使用下划线对数字进行分隔表达:

/*jdk1.6*/long a = 999999999999L ;int i = 1111111111111111;/*jdk1.7*/long a = 999_999_999_999L ;int i = 111_111111_1_111_111;/**不允许的语法*/float f1 = 3_.12f ;int i1 = 52_ ;int i2 = _51 ;double d1 = 3.12_ ;

3:switch 语句支持字符串变量:

/*jdk1.6*/switch( int | byte | char  ){case int | byte | char :  break ;default : // 代码break ; }/***jdk1.7*/switch( int | char | byte | String){case int | char | byte | String : break ;default : break ;}


4.泛型实例创建的类型推断

/**jdk1.6*/List<String>list = new ArrayList<String>() ;/**jdk 1.7*/List<String>list = new ArrayList<>() ;

5.同时捕获多个异常处理

/**jdk1.6*/try{System.out.println( "---" ) ;}catch(ArrayStoreException e){System.out.println( "异常---"   ) ;}catch(ArithmeticException e2){System.out.println( "异常---"   ) ;}/**jdk 1.7*/try{System.out.println( "---" ) ;}catch(ArrayStoreException|ArithmeticException e){System.out.println( "异常---"   ) ;}

6. 使用可变参数时,提升编译器的警告和错误信息:

List l = new ArrayList<Number>();List<String> ls = l;       // 警告信息l.add(0, new Integer(42)); //警告信息String s = ls.get(0);      // 抛出类转换异常



原创粉丝点击