-
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; } } public class NewFutureOverview<E> { /** * --4-- * 新增的类型安全检查 * @SafeVarags */ @SafeVarargs public static <T> void addToList3 (List<T> listArg, T... elements) { for (T x : elements) { listArg.add(x); } } /** * --5-- * try-with-resources能够确保需要关闭的资源在声明结束后被关闭 * 但是需要被关闭的资源必须实现AutoCloseable接口 */ public static void testTryWithResources(){ Path path = Paths.get("e:/logs","access.log"); try { //public class BufferedReader extends Reader //public abstract class Reader implements Readable, Closeable //public interface Closeable extends AutoCloseable BufferedReader br = Files.newBufferedReader(path, Charset.defaultCharset()); System.out.println(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } /** * --6-- * 关于对异常的操作 * 在java 1.7以前的版本一个方法中只能抛出一次异常,1.7支持多个 * 在java 1.7可以在catch捕获多个异常,且看下面的例子 * @throws FirstException * @throws SecondException */ public static void testException(String result) throws FirstException, SecondException{ /* * 选择抛出异常 * 有如下的异常类 * class FirstException extends Exception * class SecondException extends Exception */ if(result.equals("first")){ throw new FirstException(); }else{ throw new SecondException(); } /* * 调用testException这个方法的时候,我们可以用下面的写法 * 注意异常的捕获方式 */ /*try { testException("first"); } catch (FirstException | SecondException e) { System.out.println(e.getMessage()); }*/ } }
-