New features of jdk
1 Overview
每个版本的jdk软件都提供了一些新语法,有的语法不需要,有的语法很实用很重要.
2. Emphasis on grammar
JDK 5
自动装箱拆箱
foreach
*可变长参数(Varargs)
泛型
JDK 7
switch 语句支持 String:switch语句块中允许以字符串作为分支条件
类型推断:创建泛型对象时应用类型推断
*try-with-resources:自动关闭流
*catch 多个类型异常:一个语句块中捕获多种异常
JDK 8(2014-3-18)
*Lambda 表达式 ? Lambda允许把函数作为一个方法的参数(函数作为参数传递进方法中)。
3. Test variable length parameters
package cn.tedu.ex;
import java.util.Arrays;
//测试 jdk的新特性
// 1.5 --可变长 参数(Varargs)
public class TestJDK {
public static void main(String[] args) {
// method();
method(5);
method(5,10);
method(5,10,20);
method(5,10,20,6);
}
//TODO 优化: 提供一个method() ,但是可以动态 匹配 N 个参数
//TODO jdk1.5提供了可变长度的参数 ,根部不限制参数的个数.几个都行.可以省略 ,语法就是 类型后面加...
public static void method(int a,int... b){
//TODO 可变参数,本质上 是个数组,可以没有也可以有多个
//TODO 细节:可变参数 必须是参数列表的最后一个,这时,会把第一个参数匹配给a,剩下的都给b
// System.out.println(a);//[I@1b6d3586
System.out.println( Arrays.toString(b) );
}
//方法重载:在同一个类里的现象. 方法名相同,但是参数列表(类型/个数)不同的现象--因为灵活
// public static void method(int a){
// System.out.println(a+a);
// }
// public static void method(int a,int b){
// System.out.println(a+b);
// }
// public static void method(int a,int b,int c){
// System.out.println(a+b+c);
// }
// public static void method(int a,int b,int c,int d){
// System.out.println(a+b+c+d);
// }
}
4. Test automatic resource management
package cn.tedu.ex;
import java.io.*;
//测试 jdk的新特性
//jdk1.7 -- try-with-resources:自动关闭流 -- 自动资源管理
public class TestIO {
public static void main(String[] args) {
// method();//字节流读取
method2();
}
public static void method2() {
//TODO jdk1.7 新语法实现了IO的自动资源管理:
// 优化了释放资源的动作,直接把声明的代码放在try的小括号里就行
try ( InputStream in = new BufferedInputStream(new FileInputStream("")); ) {
//1,创建对象
//2,正式读取
int b = in.read() ;
System.out.println("b = " + b);
}catch (Exception e){
e.printStackTrace();
}
}
//字节流读取
public static void method() {
InputStream in = null ;//2,声明变量,因为finally也要用
try {
//1,创建对象
in = new BufferedInputStream(new FileInputStream(""));
//2,正式读取
int b = in.read() ;
System.out.println("b = " + b);
//TODO jdk1.7新语法提供 : 可以一次catch多个类型 的异常 ,中间用 | 分隔
}catch (ArithmeticException | IOException e){
e.printStackTrace();
} finally {
//1,写一定要被执行的代码,否则发生异常时,异常后面的代码根部不会执行!!
//3,释放资源
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
5. Test Lambda expression
package cn.tedu.ex;
//测试 jdk的新特性
//1.8 --Lambda表达式
//使用Lambda语法来代替 匿名内部类,代码不仅简洁,而且还可读
//语法: (参数列表) -> { 方法体 }
//要求: --只有一个抽象方法的 接口
public class TestLambda {
public static void main(String[] args) {
//匿名内部类
// Inter in = new Inter(){
// //提供重写的抽象方法
// @Override
// public void save() {
// System.out.println("数据保存成功~");
// }
// } ;
// in.save();
//TODO Lambda表达式--要求接口里只能有一个抽象方法--优化匿名内部类
//TODO 语法:(参数列表) -> { 方法体 } ; --练习:没有参数,没有返回值
Inter in2 = () -> {
System.out.println("Lambda表达式!"); } ;
in2.save();
//TODO 语法:(参数列表) -> { 方法体 } ; --练习:有参数,没有返回值
Inter2 in3 = (int a) -> {
System.out.println(a); } ;
in3.delete(10);
//TODO 语法:(参数列表) -> { 方法体 } ; --练习:有参数,有返回值(使用return关键字)
Inter3 in4 = (String a,int b) -> {
return a+b ; } ;
System.out.println( in4.get("陈子枢",18) );//打印获取到的结果
}
}
interface Inter3{
String get(String name,int age);
}
interface Inter2{
void delete(int id) ;
}
interface Inter{
void save() ;
}
Regular expression regex
1 Overview
就是用事先定义好的一些特定字符、及这些特定字符的组合,组成一个“规则字符串”,这个“规则字 符串”用来表达对字符串的一种过滤逻辑
对于字符串规定好的一个规则
2. Grammar
[0-9][a-z][A-Z] --是指可以出现范围内的数据,只能出现1次
{n} --出现指定的n次
[0-9]{10} --出现数字0-9范围内的就可以,可以出现10次
3. Test
package cn.tedu.ex;
import java.util.Scanner;
//测试 正则表达式
// 需求: 输入正确的手机号码
public class TestRegex {
public static void main(String[] args) {
//接收输入的手机号
String input = new Scanner(System.in).nextLine() ;
//判断,格式正确吗?---正则表达式--规定正确的字符串的格式
String regex = "[1][0-9]{10}" ;
//matches()用来判断是否与指定的正则表达式匹配,如果匹配返回true
if( input.matches(regex) )
System.out.println("手机格式正确...");
else
System.out.println("手机格式不正确...");
}
}
Extensions:
-concurrent (concurrent package)
-Stream API -Understanding
hexadecimal
-Binary/Decimal