java7新特性之新语法1

来源:互联网 发布:中国软件评测中心面试 编辑:程序博客网 时间:2024/04/29 08:12

由于种种限制只能将此节分两部分发上来,读者可放在一个类中看

public class NewFutureOverview<E> {private E e;/** * --1-- * 测试二进制 * 可以将byte,short,int,long整形直接写成2进制方式 * 可以用下划线,对下划线的用法用正常思维理解就行了 */public static void testBinary(){//An 8-bit 'byte' value:byte c = (byte)0B0000_1111;//或者可以这样写byte c = (byte)0B00000001可以有下划线也可以没有System.out.println(c);// A 16-bit 'short' value:short aShort = (short)0b0010000101000101;// Some 32-bit 'int' values:int anInt1 = 0b10100001010001011010000101000101;// A 64-bit 'long' value. Note the "L" suffix:long aLong = 0b1010000101000101101000010100010110100001010001011010000101000101L;System.out.println(aShort+","+anInt1+","+aLong);}/** * --2-- * switch支持字符串,以前的 版本是接受能转换成整形的类型 */public static void testSwitch(){String type="apple";switch(type){case "apple":System.out.println("dear");break;case "amd":System.out.println("amd");break;case "lenovo":System.out.println("le");break;}}/** * --3-- * 在java7中对泛型的改进 */public static void testGenericClass(){//1、java7中泛型的定义可以用如下的方式//java会根据定义的类型来推断// Java SE7 只支持有限的类型推断:只有构造器的参数化类型在上下文中被显著的声明了,//你才可以使用类型推断,否则不行//所以这种写法是错误的: nameList.addAll(new ArrayList<>());List<String> nameList = new ArrayList<>(16);nameList.add("Terry");//另外java7中泛型类也可以用如下定义,参看下面的方法//NewFutureOverview<Integer> newf = new NewFutureOverview<>("");}/** * 构造方法 * @param t */public <T> NewFutureOverview(T t){System.out.println(t.getClass().getName());}public E getE(){return e;}public void setE(E e){this.e = e;}}