• jdk1.7和jdk1.8区别


    在jdk7的新特性方面主要有一下几方面的增强

    本文是我学习了解了jdk7和jdk8的一些新特性的一些资料,有兴趣的大家可以浏览下下面的内容。官方文档

    • 在jdk7的新特性方面主要有下面几方面的增强:
    1. jdk1.7语法上

      1.1 二进制变量的表示,支持将整数类型用二进制来表示,以0b开头。

      所有整数int、short、long、byte都可以用二进制表示

      // An 8-bit 'byte' value:
      byte aByte = (byte) 0b00100001;
      
       ```
      

      // A 16-bit 'short' value:
      short aShort = (short) 0b1010000101000101;

      
      

      // Some 32-bit 'int' values:
      intanInt1 = 0b10100001010001011010000101000101;
      intanInt2 = 0b101;
      intanInt3 = 0B101; // The B can be upper or lower case.

      
      

      // A 64-bit 'long' value. Note the "L" suffix:
      long aLong = 0b1010000101000101101000010100010110100001010001011010000101000101L;

      
      

      // 二进制在数组等的使用
      final int[] phases = { 0b00110001, 0b01100010, 0b11000100, 0b10001001,
      0b00010011, 0b00100110, 0b01001100, 0b10011000 };

      
      1.2 Switch语句支持String类型
      
      

      public void String getTypeOfDayWithSwitchStatement(String dayOfWeekArg) {
      String typeOfDay;
      switch (dayOfWeekArg) {
      case "Monday":
      typeOfDay = "Start of work week";
      break;
      case "Tuesday":
      case "Wednesday":
      case "Thursday":
      typeOfDay = "Midweek";
      break;
      case "Friday":
      typeOfDay = "End of work week";
      break;
      case "Saturday":
      case "Sunday":
      typeOfDay = "Weekend";
      break;
      default:
      throw new IllegalArgumentException("Invalid day of the week: " + dayOfWeekArg);
      }
      return typeOfDay;
      }

      
      1.3 Try-with-resource语句
      
      > 注意:实现java.long.AutoCloseable接口的资源都可以放到try中,
      	  跟final里面的关闭资源类似;按照声明逆序关闭资源;Try块抛出的异常Throwable.getSuppressed获取。
      
      
       	  try (java.util.zip.ZipFile zf = new java.util.zip.ZipFile(zipFileName);
        java.io.BufferedWriter writer = java.nio.file.Files 
        .newBufferedWriter(outputFilePath, charset)) {
        // Enumerate each entry
        for (java.util.Enumeration entries = zf.entries(); entries
        .hasMoreElements();) {
        // Get the entry name and write it to the output file
        String newLine = System.getProperty("line.separator");
        String zipEntryName = ((java.util.zip.ZipEntry) entries
        .nextElement()).getName() + newLine;
        writer.write(zipEntryName, 0, zipEntryName.length());
        }
        }
       	  ```
    
        1.4 Catch多个异常 说明:Catch异常类型为final; 生成Bytecode 会比多个catch小; Rethrow时保持异常类型
       	
       	```
       	public static void main(String[] args) throws Exception {
         	try {
        	testthrows();
        	} catch (IOException | SQLException ex) {
        	throw ex;
        		}
        	}
        public static void testthrows() throws IOException, SQLException {
        }
       	```
       	
       	1.5 数字类型的下划线表示 更友好的表示方式,不过要注意下划线添加的一些标准,可以参考下面的示例
       	
       	```
       	long creditCardNumber = 1234_5678_9012_3456L;
        long socialSecurityNumber = 999_99_9999L;
        float pi = 3.14_15F;
        long hexBytes = 0xFF_EC_DE_5E;
        long hexWords = 0xCAFE_BABE;
        long maxLong = 0x7fff_ffff_ffff_ffffL;
        byte nybbles = 0b0010_0101;
        long bytes = 0b11010010_01101001_10010100_10010010; 
        //float pi1 = 3_.1415F;      // Invalid; cannot put underscores adjacent to a decimal point
        //float pi2 = 3._1415F;      // Invalid; cannot put underscores adjacent to a decimal point
        //long socialSecurityNumber1= 999_99_9999_L;         // Invalid; cannot put underscores prior to an L suffix 
        //int x1 = _52;              // This is an identifier, not a numeric literal
        int x2 = 5_2;              // OK (decimal literal)
        //int x3 = 52_;              // Invalid; cannot put underscores at the end of a literal
        int x4 = 5_______2;        // OK (decimal literal) 
        //int x5 = 0_x52;            // Invalid; cannot put underscores in the 0x radix prefix
        //int x6 = 0x_52;            // Invalid; cannot put underscores at the beginning of a number
        int x7 = 0x5_2;            // OK (hexadecimal literal)
        //int x8 = 0x52_;            // Invalid; cannot put underscores at the end of a number 
        int x9 = 0_52;             // OK (octal literal)
        int x10 = 05_2;            // OK (octal literal)
        //int x11 = 052_;            // Invalid; cannot put underscores at the end of a number 
       	```
       	
       	1.6 泛型实例的创建可以通过类型推断来简化 可以去掉后面new部分的泛型类型,只用<>就可以了。
       	
       	```
       	List strList = new ArrayList(); 
        List<String> strList4 = new ArrayList<String>(); 
        List<Map<String, List<String>>> strList5 =  new ArrayList<Map<String, List<String>>>();
       	```
       	
       	```
       	//编译器使用尖括号 (<>) 推断类型 
        List<String> strList0 = new ArrayList<String>(); 
        List<Map<String, List<String>>> strList1 =  new ArrayList<Map<String, List<String>>>(); 
        List<String> strList2 = new ArrayList<>(); 
        List<Map<String, List<String>>> strList3 = new ArrayList<>();
        List<String> list = new ArrayList<>();
        list.add("A");
          // The following statement should fail since addAll expects
          // Collection<? extends String>
        //list.addAll(new ArrayList<>()); 
        ```
        
        1.7 在可变参数方法中传递非具体化参数,改进编译警告和错误
        
        Heap pollution 指一个变量被指向另外一个不是相同类型的变量。例如
        
        ```
        List l = new ArrayList<Number>();
        List<String> ls = l;       // unchecked warning
        l.add(0, new Integer(42)); // another unchecked warning
        String s = ls.get(0);      // ClassCastException is thrown
        Jdk7:
        public static <T> void addToList (List<T> listArg, T... elements) {
        for (T x : elements) {
        listArg.add(x);
        }
        }
        ```
        
       > 你会得到一个warning
        warning: [varargs] Possible heap pollution from parameterized vararg type
        要消除警告,可以有三种方式
        1.加 annotation @SafeVarargs
        2.加 annotation @SuppressWarnings({"unchecked", "varargs"})
        3.使用编译器参数 –Xlint:varargs;
        
        1.8  信息更丰富的回溯追踪 就是上面try中try语句和里面的语句同时抛出异常时,异常栈的信息
        
        ```
        java.io.IOException  
        *     at Suppress.write(Suppress.java:19)  
        *      at Suppress.main(Suppress.java:8)  
        *      Suppressed:  java.io.IOException 
        *          at Suppress.close(Suppress.java:24) 
        *          at Suppress.main(Suppress.java:9)  
        *      Suppressed:  java.io.IOException 
        *          at  Suppress.close(Suppress.java:24)  
        *          at  Suppress.main(Suppress.java:9) 
        ```
        
    2. ThreadLocalRandon 并发下随机数生成类,保证并发下的随机数生成的线程安全,实际上就是使用threadlocal
    
    

    final int MAX = 100000;
    ThreadLocalRandom threadLocalRandom = ThreadLocalRandom.current();
    long start = System.nanoTime();
    for (int i = 0; i < MAX; i++) {
    threadLocalRandom.nextDouble();
    }
    long end = System.nanoTime() - start;
    System.out.println("use time1 : " + end);
    long start2 = System.nanoTime();
    for (int i = 0; i < MAX; i++) {
    Math.random();
    }
    long end2 = System.nanoTime() - start2;
    System.out.println("use time2 : " + end2);

  • 相关阅读:
    互联网与局域网(四)
    Socket介绍(五)
    HttpClient(七)
    TCP协议与HTTP协议区别
    TCP连接的三次握手
    context-param和init-param区别
    【HPU】[1736]老王修马路(二)
    【HPU】[1735]老王修马路(一)
    【HPU】[1734]老王修公园
    【HPU】[1733]神奇的数字9
  • 原文地址:https://www.cnblogs.com/f-s-q/p/6597742.html
Copyright © 2020-2023  润新知