java7新特性

  1 package com.test;
  2
  3 import java.io.BufferedReader;
  4 import java.io.FileReader;
  5 import java.io.IOException;
  6 import java.sql.Connection;
  7 import java.sql.DriverManager;
  8 import java.sql.PreparedStatement;
  9 import java.sql.ResultSet;
 10 import java.sql.ResultSetMetaData;
 11 import java.sql.SQLException;
 12 import java.util.ArrayList;
 13 import java.util.List;
 14
 15 /**
 16  *
 17  * @author 测试java7的新特性
 18  */
 19 public class TestJava7
 20 {
 21     // 1. 支持二进制字面值
 22     public static void binaryLiteral()
 23     {
 24         int a = 0b100;
 25         int b = 0B1100;
 26         int c = a & b;
 27         System.out.println(c);
 28     }
 29
 30     // 2. 数字字面值中添加下划线"_"分割
 31     public static void underlineInNumber()
 32     {
 33         /*
 34          * <注意: 数字的开头结尾不能添加; 小数点前后不能添加; F或L前面不能添加>
 35          */
 36         int a = 1_2_3;
 37         long b = 92_3L;
 38         double c = 3.14_15926;
 39         double d = 3.1_415F;
 40         System.out.println(a);
 41         System.out.println(b);
 42         System.out.println(c);
 43         System.out.println(d);
 44     }
 45
 46     // 3. switch表达式支持string常量
 47     public static void switchSupportString()
 48     {
 49         String str = "ccc";
 50         switch (str)
 51         {
 52         case "aaa":
 53             System.out.println("I‘m first");
 54             break;
 55         case "bbb":
 56             System.out.println("I‘m second");
 57             break;
 58         case "ccc":
 59             System.out.println("I‘m third");
 60             break;
 61         default:
 62             System.out.println("not matched");
 63             break;
 64         }
 65     }
 66
 67     //4. try-with-resources特性支持自动关闭实现了java.lang.AutoCloseable和java.io.Closeable接口的对象资源
 68     // 一个 try-with-resources 语句可以像普通的 try 语句那样有 catch 和 finally 块。
 69     // 在try-with-resources 语句中, 任意的 catch 或者 finally 块都是在声明的资源被关闭以后才运行
 70     public static void tryWithResources()
 71     {
 72         try
 73         {
 74             Class.forName("com.mysql.jdbc.Driver");
 75             // 将对象声明在try()中
 76             try (Connection connection = DriverManager.getConnection(
 77                     "jdbc:mysql://172.17.13.38:3306/g2mdev", "root", "");
 78
 79                     PreparedStatement preparedStatement = connection
 80                             .prepareStatement("select SQL_CALC_FOUND_ROWS * from g2m_sec_account where id = ? or id = ? or id = ?");)
 81             {
 82                 preparedStatement.setString(1, "1");
 83                 preparedStatement.setString(2, "2");
 84                 preparedStatement.setString(3, "3");
 85
 86                 try (ResultSet resultSet = preparedStatement.executeQuery())
 87                 {
 88                     ResultSetMetaData meta = resultSet.getMetaData();
 89                     for (int i = 0; i < meta.getColumnCount(); ++i)
 90                     {
 91                         System.out.printf("%20s  ", meta.getColumnName(i + 1));
 92                     }
 93                     System.out.println();
 94                     while (resultSet.next())
 95                     {
 96                         for (int i = 0; i < meta.getColumnCount(); ++i)
 97                         {
 98                             System.out.printf("%20s  ", resultSet
 99                                     .getString(meta.getColumnName(i + 1)));
100                         }
101                         System.out.println();
102                     }
103                 }
104             }
105             catch (SQLException e)
106             {
107                 // TODO Auto-generated catch block
108                 e.printStackTrace();
109             }
110         }
111         catch (ClassNotFoundException e)
112         {
113             e.printStackTrace();
114         }
115     }
116
117     //5. 一个catch块捕获多个异常
118     public static void multiCatch()
119     {
120         BufferedReader bufferedReader;
121         try
122         {
123             bufferedReader = new BufferedReader(new FileReader(
124                     "d:/note.txt"));
125             String buffer = null;
126             while (null != (buffer = bufferedReader.readLine()))
127             {
128                 System.out.println(buffer);
129             }
130             bufferedReader.close();
131             throw new MyException("-------ERROR-------");
132         }
133         catch (IOException | MyException e)
134         {
135             e.printStackTrace();
136         }
137     }
138
139     //6. 创建泛型对象时类型推断
140     public static void genericTypeInfer()
141     {
142         List<String> list = new ArrayList<>();
143         list.add("aaa");
144         list.add("bbb");
145     }
146     public static void main(String[] args)
147     {
148         binaryLiteral();
149         System.out.println("-------------");
150         underlineInNumber();
151         System.out.println("-------------");
152         switchSupportString();
153         System.out.println("-------------");
154         tryWithResources();
155         System.out.println("-------------");
156     }
157 }
158 class MyException extends Exception
159 {
160     private static final long serialVersionUID = 884387500985353045L;
161
162     public MyException(String string)
163     {
164         super(string);
165     }
166 }
时间: 2024-11-15 10:14:16

java7新特性的相关文章

java7新特性之Try-with-resources (TWR)

java7新特性之Try-with-resources (TWR) This change is easy to explain, but it has proved to have hidden subtleties, which made it much less easy to implement than originally hoped. The basic idea is to allow a resource (for example, a file or something a

java7新特性之Diamond syntax

java7新特性之Diamond syntax Java 7 also introduces a change that means less typing for you when dealing with generics. One of the problems with generics is that the definitions and setup of instances can be really verbose. Let's suppose that you have som

java7新特性之Simplified varargs method invocation

java7新特性之Simplified varargs method invocation This is one of the simplest changes of all-it moves a warning about type information for a very specific case where varargs combines with generics in a method signature. Put another way, unless you're in

小菜修炼之道——Java7新特性

1.TWR 自动关闭资源 自动关闭资源必须要注意书写规范,所有需要关闭的资源必须使用一个变量声明出来并且写在try的小括号块中,不是大括号.如下代码: try(  InputStream is = new FileInputStream(new File(path))    ){ // 可能发生异常的代码 }catch(Exception e){ // 异常信息捕获 }finally{ // 自动关闭资源,无需手动处理 is } 2.switch 新增字符串支持 在java7之前switch只是

java7新特性(简述八大新特性)

1.switch中添加对String类型的支持 public String generate(String name, String gender) { String title = ""; switch (gender) { case "男": title = name + " 先生"; break; case "女": title = name + " 女士"; break; default: titl

Java之Java7新特性之try资源句式

一.原来写法: 1 static String readFirstLineFromFile(String path) throws IOException { 2 BufferedReader br = null; 3 try { 4 br = new BufferedReader(new FileReader(path)); 5 } catch (Exception ex) { 6 //do exception action 7 } finally { 8 if (br != null) {

java7 新特性 总结版

http://www.zhuke.com/user/zkuser57546968?p6=123.baidu.com=&lvhttp://www.zhuke.com/user/zkuser59918842?p6=123.baidu.com=&lvhttp://www.zhuke.com/user/zkuser56775905?p6=123.baidu.com=&lvhttp://www.zhuke.com/user/zkuser71200347?p6=123.baidu.com=&a

java7和java8新特性

以下来至网址: http://blog.csdn.net/samjustin1/article/details/52268004 Java7 新特性 1.switch中可以使用字符串了 String s = "test"; switch (s) { case "test" : System.out.println("test"); case "test1" : System.out.println("test1&qu

java5、java6、java7、java8的新特性

Java5: 1.泛型 Generics:        引用泛型之后,允许指定集合里元素的类型,免去了强制类型转换,并且能在编译时刻进行类型检查的好处. Parameterized Type作为参数和返回值,Generic是vararg.annotation.enumeration.collection的基石. A.类型安全 抛弃List.Map,使用List<T>.Map<K,V>给它们添加元素或者使用Iterator<T>遍历时,编译期就可以给你检查出类型错误 B