JDK8~13新特性(附帶資源下載)
JDK8新增的日期處理,自帶加解密,Optional特性,lambada表達式,自定義的函數接口,四大接口Funtion,Consumer,Supplier,Predicate的使用,stream流的編程,核心方法map,fitter,reduce,match使用
JDK9新工具Jshell和增強API
JDK10局部變量var的使用
JDK11新標準HttpClient提交Post和Get請求
JDK13新特性文本塊和增強switch
JDK8特性解讀
- JDK8下載
window64位下載 密碼:6cyj
Linux64位下載 密碼:1q5p
Mac64位下載 密碼:vqua
default關鍵字
在以前的接口裏只能有抽象方法,不能有任何方法的實現,在jdk1.8中引入了新的關鍵字default,使用default修飾方法可以在接口裏面定義具體的方法實現
默認方法:在接口裏定義一個默認方法,接口的實現類實現的這個接口後,不用管default修飾的方法就可以直接調用。即是接口的默認方法實現
public interface Animal {
void run();
void eat();
default void breath(){
System.out.println("生物都需要呼吸");
}
}
public class Dog implements Animal {
@Override
public void run() {
System.out.println("狗");
}
@Override
public void eat() {
System.out.println("狗");
}
新增Base6加解密API
Base64是一種基於64個可打印字符來表示二進制數據的方法,基於64個字符A~Z,a~z,0~9,+,/的編碼方式,二進制數據和字符串資料之間可以相互轉換。
舊版API:
BASE64Encoder encoder=new BASE64Encoder();
BASE64Decoder decoder=new BASE64Decoder();
String text="加密前數據";
byte[] bytes=text.getBytes();
//加密
String encodertext=encoder.encode(bytes);
System.out.println(encodertext);
//解碼
System.out.println(new String(decoder.decodeBuffer(encodertext),"UTF-8"));
5Yqg5a+G5YmN5pWw5o2u
加密前數據
Process finished with exit code 0
缺點:解碼和編碼的效率比較差,以後的版本會被取消
新版API:
Base64.Decoder decoder= Base64.getDecoder();
Base64.Encoder encoder=Base64.getEncoder();
String text="加密前數據";
byte[] bytes=text.getBytes();
String encodetext=encoder.encodeToString(bytes);
System.out.println(encodetext);
System.out.println(new String(decoder.decode(encodetext),"UTF-8"));
5Yqg5a+G5YmN5pWw5o2u
加密前數據
Process finished with exit code 0
- 日期處理類
Java.util.Date是非線程安全的API,設計比較差,時間加減,比較麻煩,Java8 通過新發布的Date-time API(JSR310)來進一步加強時間和日期的處理,新增了很多常見的API,日期時間的比較,加減,格式化等
核心類
LocalDate:不包含時間的日期
LocalTime:不含日期的時間
LocalDateTime:包含了日期和時間
LocalDate常見的API
LocalDate today = LocalDate.now();
System.out.println("今天日期:"+today);
//獲取年月日,周幾
System.out.println("現在是那年:"+today.getYear());
System.out.println("現在是那月:"+today.getMonth());
System.out.println("現在是那日:"+today.getMonthValue());
System.out.println("現在是幾號:"+today.getDayOfMonth());
System.out.println("現在是周幾:"+today.getDayOfWeek());
LocalDate changeDate = today.plusYears(1);
System.out.println("加後是哪年年:"+changeDate.getYear());
System.out.println("舊的是哪年年:"+today.getYear());
//⽇日期⽐比較
System.out.println("isAfter: "+changeDate.isAfter(today));
//getYear() int 獲取當前⽇日期的年份
//getMonth() Month 獲取當前⽇日期的⽉份對象
//getMonthValue() int 獲取當前⽇日期是第幾⽉
//getDayOfWeek() DayOfWeek 表示該對象表示的⽇日期是星期幾
//getDayOfMonth() int 表示該對象表示的⽇日期是這個⽉月第幾天
//getDayOfYear() int 表示該對象表示的⽇日期是今年年第⼏天
//plusYears(long yearsToAdd) LocalDate 當前對象增加指定的年年份數
//plusMonths(long monthsToAdd) LocalDate 當前對象增加指定的⽉月份數
//plusWeeks(long weeksToAdd) LocalDate 當前對象增加指定的週數
//minusMonths(long monthsToSubtract) LocalDate 當前對象減去註定的月數
//isAfter(ChronoLocalDate other) boolean 比較當前對象⽇日期是否在other對象⽇日期之後
//isEqual(ChronoLocalDate other) boolean 比較兩個⽇日期對象是否相等
輸出結果:
今天日期:2019-11-19
現在是那年:2019
現在是那月:NOVEMBER
現在是那日:11
現在是幾號:19
現在是周幾:TUESDAY
加後是哪年年:2020
舊的是哪年年:2019
isAfter: true
Process finished with exit code 0
日期格式化和獲得指定日期
LocalDateTime ldt = LocalDateTime.of(2020, 11, 11, 8, 20, 30);
System.out.println(ldt);
輸出結果:
2020-11-11T08:20:30
Process finished with exit code 0
計算日期時間差
LocalDateTime today = LocalDateTime.now();
System.out.println(today);
LocalDateTime changeDate = LocalDateTime.of(2020,10,1,10,40,30);
System.out.println(changeDate);
Duration duration = Duration.between( today,changeDate);
//第⼆二個參數減第⼀一 個參數
System.out.println(duration.toDays());//兩個時間差的天數
System.out.println(duration.toHours());//兩個時間差的⼩小時數
System.out.println(duration.toMinutes());//兩個時間差的分鐘數
System.out.println(duration.toMillis());//兩個時間差的毫秒數
System.out.println(duration.toNanos());//兩個時間差的納秒數
輸出結果:
2019-11-19T14:33:29.947
2020-10-01T10:40:30
316
7604
456247
27374820053
27374820053000000
Process finished with exit code 0
Optional類
主要解決空指針異常(NullPointerException),本質是一個有可選值的包裝類,這意味着Optional類j既可以含有對象也可以爲空
- of()
null 值作爲參數傳遞進去,則會拋異常
Optional<Student> opt = Optional.of(user);
- ofNullable()
如果對象即可能是null也可能是非null,應該使用ofNullable()方法
Optional<Student> opt = Optional.ofNullable(user);
訪問Optional對象的值
- get()方法
Optional<Student> opt = Optional.ofNullable(student);
Student s = opt.get();
- 如果值存在isPresent()方法返回true,調用get()方法會返回該對象一般使用get之前需要驗證是否有值
public static void main(String[] args) {
Student student = null;
test(student);
}
public static void test(Student student){
Optional<Student> opt = Optional.ofNullable(student);
System.out.println(opt.isPresent());
}
- 兜底orElse方法
如果有值就返回該值,否則返回傳遞給他的參數值
Student student1 = null;
Student student2 = new Student(2);
Student result = Optional.ofNullable(student1).orElse(student2);
System.out.println(result.getAge());
Student student = null;
int result = Optional.ofNullable(student).map(obj- >obj.getAge()).orElse(4);
System.out.println(result);