jdk14实用新特性
1.instanceof的模式匹配(预览)
package com.atguigu.feature;
import org.junit.Test;
/**
* 1. JEP 305:instanceof的模式匹配(预览)
*
* @author shkstart Email:shkstart@126.com
* @create 上午 11:32
*/
public class Feature01 {
@Test
public void test1(){
Object obj = new String("hello,Java14");
obj = null;//在使用null 匹配instanceof 时,返回都是false.
if(obj instanceof String){
String str = (String) obj;
System.out.println(str.contains("Java"));
}else{
System.out.println("非String类型");
}
//举例1:
if(obj instanceof String str){ //新特性:省去了强制类型转换的过程
System.out.println(str.contains("Java"));
}else{
System.out.println("非String类型");
}
}
}
// 举例2
class InstanceOf{
String str = "abc";
public void test(Object obj){
if(obj instanceof String str){//此时的str的作用域仅限于if结构内。
System.out.println(str.toUpperCase());
}else{
System.out.println(str.toLowerCase());
}
}
}
//举例3:
class Monitor{
private String model;
private double price;
// public boolean equals(Object o){
// if(o instanceof Monitor other){
// if(model.equals(other.model) && price == other.price){
// return true;
// }
// }
// return false;
// }
public boolean equals(Object o){
return o instanceof Monitor other && model.equals(other.model) && price == other.price;
}
}
2.NullPointerException
package com.atguigu.feature;
/**
* 使用的NullPointerException的提示
* -XX:+ShowCodeDetailsInExceptionMessages
*
*
* @author shkstart Email:shkstart@126.com
* @create 下午 11:14
*/
public class Feature02 {
public static void main(String[] args) {
// Bank bank = new Bank(new Customer(new Account(1000)));
/*
Exception in thread "main" java.lang.NullPointerException: Cannot invoke
"com.atguigu.feature.Account.withdraw(double)" because the return value of
"com.atguigu.feature.Customer.getAccount()" is null
*/
Bank bank = new Bank(new Customer());
bank.getCustomer().getAccount().withdraw(200);
}
}
class Bank {
private Customer customer;
public Bank() {
}
public Bank(Customer customer) {
this.customer = customer;
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
}
class Customer {
private Account account;
public Customer() {
}
public Customer(Account account) {
this.account = account;
}
public Account getAccount() {
return account;
}
public void setAccount(Account account) {
this.account = account;
}
}
class Account {
private double balance;//余额
public Account() {
}
public Account(double balance) {
this.balance = balance;
}
//取钱操作
public void withdraw(double amt) {
if (balance >= amt) {
balance -= amt;
System.out.println("成功取款:" + amt);
} else {
System.out.println("余额不足,取款失败");
}
}
}
3.Record(预览特性)
package com.atguigu.feature;
/**
* @author shkstart Email:shkstart@126.com
* @create 下午 6:20
*/
public record Person(String name,Person partner) {
//还可以声明静态的属性、静态的方法、构造器、实例方法
public static String nation;
public static String showNation(){
return nation;
}
public Person(String name){
this(name,null);
}
public String getNameInUpperCase(){
return name.toUpperCase();
}
//不可以声明非静态的属性
// private int id;//报错
}
//不可以将record定义的类声明为abstract的
//abstract record Order(){
//
//}
//不可以给record定义的类声明显式的父类(非Record类)
//record Order() extends Thread{
//
//}
package com.atguigu.feature;
import org.junit.Test;
import java.util.HashSet;
/**
* Record类型的演示
*
* @author shkstart Email:shkstart@126.com
* @create 下午 6:13
*/
public class Feature03 {
@Test
public void test1(){
//测试构造器
Person p1 = new Person("罗密欧",new Person("zhuliye",null));
//测试toString()
System.out.println(p1);
//测试equals():
Person p2 = new Person("罗密欧",new Person("zhuliye",null));
System.out.println(p1.equals(p2));
//测试hashCode()和equals()
HashSet<Person> set = new HashSet<>();
set.add(p1);
set.add(p2);
for (Person person : set) {
System.out.println(person);
}
//测试name()和partner():类似于getName()和getPartner()
System.out.println(p1.name());
System.out.println(p1.partner());
}
@Test
public void test2(){
Person p1 = new Person("zhuyingtai");
System.out.println(p1.getNameInUpperCase());
Person.nation = "CHN";
System.out.println(Person.showNation());
}
}
4.switch表达式
package com.atguigu.feature;
import org.junit.Test;
/**
* switch的新特性
*
* @author shkstart Email:shkstart@126.com
* @create 下午 12:10
*/
public class Feature04 {
//jdk12之前的用法
@Test
public void test1() {
Week day = Week.FRIDAY;
switch (day) {
case MONDAY:
case TUESDAY:
case WEDNESDAY:
System.out.println(1);
break;
case THURSDAY:
System.out.println(2);
break;
case FRIDAY:
case SATURDAY:
System.out.println(3);
break;
case SUNDAY:
System.out.println(4);
break;
default:
throw new IllegalStateException("What day is today?" + day);
}
}
//jdk12新特性:引用switch表达式
@Test
public void test2(){
Week day = Week.FRIDAY;
switch (day){
case MONDAY,TUESDAY,WEDNESDAY -> System.out.println(1);
case THURSDAY -> System.out.println(2);
case FRIDAY,SATURDAY -> System.out.println(3);
case SUNDAY -> System.out.println(4);
default -> throw new IllegalStateException("What day is today?" + day);
}
//使用变量接收switch表达式的值
int num = switch (day) {
case MONDAY, TUESDAY, WEDNESDAY -> 1;
case THURSDAY -> 2;
case FRIDAY, SATURDAY -> 3;
case SUNDAY -> 4;
default -> throw new IllegalStateException("What day is today?" + day);
};
System.out.println(num);
}
//jdk13新特性:引用了yield关键字,用于返回指定的数据,结束switch结构
@Test
public void test3(){
String x = "3";
int num = switch (x){
case "1" -> 1;
case "2" -> 2;
case "3" -> 3;
default -> {
System.out.println("default...");
yield 4;
}
};
System.out.println(x);
}
@Test
public void test4(){
String x = "3";
int num = switch (x){
case "1":yield 1;
case "2":yield 2;
case "3":yield 3;
default: yield 4;
};
System.out.println(num);
}
}
enum Week {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY;
}
5.文本块(预览第二版)
package com.atguigu.feature;
import org.junit.Test;
/**
* 文本块(text blocks)的使用
*
* @author shkstart Email:shkstart@126.com
* @create 下午 7:13
*/
public class Feature05 {
@Test
public void test1(){
String text1 = "The Sound of silence\n" +
"Hello darkness, my old friend\n" +
"I've come to talk with you again\n" +
"Because a vision softly creeping\n" +
"Left its seeds while I was sleeping\n" +
"And the vision that was planted in my brain\n" +
"Still remains\n" +
"Within the sound of silence";
System.out.println(text1);
//jdk13中的新特性:
String text2 = """
The Sound of silence
Hello darkness, my old friend
I've come to talk with you again
Because a vision softly creeping
Left its seeds while I was sleeping
And the vision that was planted in my brain
Still remains
Within the sound of silence
""";
System.out.println();
System.out.println(text2);
System.out.println(text1.length());
System.out.println(text2.length());
}
//html
@Test
public void test2(){
String html1 = "<html lang=\"en\">\n" +
"<head>\n" +
" <meta charset=\"UTF-8\">\n" +
" <title>java14新特性</title>\n" +
"</head>\n" +
"<body>\n" +
" <p>hello,atguigu</p>\n" +
"</body>\n" +
"</html>";
//jdk13中的新特性:
String html2 = """
<html lang="en">
<head>
<meta charset="UTF-8">
<title>java14新特性</title>
</head>
<body>
<p>hello,atguigu</p>
</body>
</html>
""";
}
//json
@Test
public void test3() {
//jdk13之前的写法
String myJson = "{\n" +
" \"name\":\"Song Hongkang\",\n" +
" \"address\":\"www.atguigu.com\",\n" +
" \"email\":\"shkstart@126.com\"\n" +
"}";
System.out.println(myJson);
//jdk13的新特性
String myJson1 = """
{
"name":"Song Hongkang",
"address":"www.atguigu.com",
"email":"shkstart@126.com"
}""";
System.out.println(myJson1);
}
//sql
@Test
public void test4(){
String sql = "SELECT id,NAME,email\n" +
"FROM customers\n" +
"WHERE id > 4\n" +
"ORDER BY email DESC";
//jdk13新特性:
String sql1 = """
SELECT id,NAME,email
FROM customers
WHERE id > 4
ORDER BY email DESC
""";
}
//jdk14新特性
@Test
public void test5(){
String sql1 = """
SELECT id,NAME,email
FROM customers
WHERE id > 4
ORDER BY email DESC
""";
System.out.println(sql1);
// \:取消换行操作
// \s:表示一个空格
String sql2 = """
SELECT id,NAME,email \
FROM customers\s\
WHERE id > 4 \
ORDER BY email DESC
""";
System.out.println(sql2);
}
}
6.弃用ParallelScavenge和SerialOld GC组合
7.删除CMS垃圾回收器
8.ZGC