JDK 某些特性

//正则表达式

import java.util.Scanner;

//用来判断 字符串的格式 是否符合正确的格式
public class Test_Regex {
public static void main(String[] args) {
method1();
method2();
}
private static void method1(){
//需求--校验手机号码是否正确
//1,接受用户输入的一串手机号码
System.out.println("输入手机号");
String tel = new Scanner(System.in).nextLine();
//2.利用正则表达式 校验
String regex = "[1][0-9]{10}";//11位 ==1开头
//判断tel 是否符合正则
if(tel.matches(regex))
System.out.println("格式正确。。。");
else
System.out.println("格式不正确。。。");
}

private static void method2(){
//TODO 校验身份证号码是否正确 18位 17位数字加+末位数字/X
System.out.println("输入身份证号");
String tel2 = new Scanner(System.in).nextLine();
String regex2 = "[0-9]{17}[0-9Xx]";//11位 ==1开头

//判断tel 是否符合正则
if(tel2.matches(regex2))
System.out.println("格式正确。。。");
else
System.out.println("格式不正确。。。");

}
}

jdk1.5
--自动装箱拆箱,foreach,泛型--可变长参数(Varargs)
public class Test_JDK5 {
public static void main(String[] args) {
//TODO 提供重载的add()--同一个类里--方法名相同但是参数列表不同的现象
add(1);
add(1,2);
add(1,2,3,4,5,6);
add(1,2,3,4,5,6,7,8,9);
}
//1,如果参数列表可以动态的匹配个数--jdk1.5提供的可变长参数...
//2,可变参数 必须是 参数列表里的最后一个
private static void add(int x,int... i) {
//3,可变长的参数,,,本质上就是一个数组
System.out.println(i);//[I@1b6d3586
//遍历数组
for (int i1 : i) {
System.out.println(i1);
}
}
}
jdk1.7
--switch语句支持String,类型推断,try-with-resources:自动关闭流
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;

//jdk7新特性-try-with-resources:自动关闭流
public class Test_JDK7 {
public static void main(String[] args) throws IOException {
// method();//IO 练习
method2();//IO 练习
}
//jdk7后 try-with-resources:自动关闭流
private static void method2() {
try(
InputStream in = System.in;
BufferedInputStream bis = new BufferedInputStream(in);
) {
//1,创建读取流对象
//2,开始读取
int b = 0;
//有数据就读,没数据就返回-1
while( ( b = bis.read() ) != -1){
System.out.println((char) b );
}
//jdk7提供的可以一次性catch多个 类型的异常 |分隔
}catch(IOException|ArithmeticException|ArrayIndexOutOfBoundsException e){
e.printStackTrace();
}
}
//jdk7前
//--需求:接收键盘输入的数据,并打印
public static void method() {
InputStream in = null ;
BufferedInputStream bis = null;
try {
//1,创建读取流对象
in = System.in;
bis = new BufferedInputStream(in);
//2,开始读取
int b = 0;
//有数据就读,没数据就返回-1
while( ( b = bis.read() ) != -1){
System.out.println((char) b );
}
}catch (Exception e){
e.printStackTrace();
}finally {//一定要执行的代码放finally
//3,释放资源
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

jdk1.8
--Lambda 表达式,函数式接口
//jdk8 Lambda表达式
//要求: 只针对接口可以使用, 而且接口里只能有一个抽象方法
//好处: 优化了匿名内部类的使用 -- 函数式编程
public class Test_JDK8 {
public static void main(String[] args) {
//TODO 1, 使用匿名内部类测试
A a = new A(){//匿名对象+匿名内部类
@Override
public void save() {//重写抽象方法
System.out.println("save..");
}
};
a.save();//方法调用
//TODO 2, 使用Lambda表达式优化匿名内部类
//语法: ( 参数列表 ) -> { 方法体 }
A a2 = ( ) -> { System.out.println("hello"); } ;
a2.save();
//TODO B--B接口里的get(int id)是需要参数的
B b = ( int m ) -> { System.out.println("get::"+m); } ;
b.get(1000);//把1000交给m
//TODO C--C接口的int delete(String id)需要参数有返回值
//方法的返回值通过return关键字
C c = ( String n ) -> { return 100; } ;
//把"jack"交给n,x保存返回来的100
int x = c.delete("jack");
System.out.println(x);
}
}
//TODO 提供接口
interface C{
int delete(String id);
}
interface B{
void get(int id);
}
interface A{
void save();
}




posted @ 2020-09-27 22:34  Liang-shi  阅读(116)  评论(0编辑  收藏  举报