现实问题:
在Java中,通常需要使用String类型表达HTML,XML,SQL或JSON等格式的字符串,在进行字符串赋值时需要进行转义和连接操作,然后才能编译该代码,这种表达方式难以阅读并且难以维护。
JDK13的新特性
使用"""作为文本块的开始符和结束符,在其中就可以放置多行的字符串,不需要进行任何转义。因此,文本块将提高Java程序的可读性和可写性。
基本使用:
"""
line1
line2
line3
"""
相当于:
"line1\nline2\nline3\n"
或者一个连接的字符串:
"line1\n" +
"line2\n" +
"line3\n"
如果字符串末尾不需要行终止符,则结束分隔符可以放在最后一行内容上。例如:
"""
line1
line2
line3"""
相当于
"line1\nline2\nline3"
文本块可以表示空字符串,但不建议这样做,因为它需要两行源代码:
String empty = """
""";
举例1:普通文本
原有写法:
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);
使用新特性:
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(text2);
举例2:HTML语句
<html>
<body>
<p>Hello, 尚硅谷p>
body>
html>
将其复制到Java的字符串中,会展示成以下内容:
"\n" +
" \n" +
" Hello, 尚硅谷
\n" +
" \n" +
"\n";
即被自动进行了转义,这样的字符串看起来不是很直观,在JDK 13中:
"""
Hello, world
""";
举例3:SQL语句
select employee_id,last_name,salary,department_id
from employees
where department_id in (40,50,60)
order by department_id asc
原有方式:
String sql = "SELECT id,NAME,email\n" +
"FROM customers\n" +
"WHERE id > 4\n" +
"ORDER BY email DESC";
使用新特性:
String sql1 = """
SELECT id,NAME,email
FROM customers
WHERE id > 4
ORDER BY email DESC
""";
举例4:JSON字符串
原有方式:
String myJson = "{\n" +
" \"name\":\"Song Hongkang\",\n" +
" \"address\":\"www.atguigu.com\",\n" +
" \"email\":\"[email protected]\"\n" +
"}";
System.out.println(myJson);
使用新特性:
String myJson1 = """
{
"name":"Song Hongkang",
"address":"www.atguigu.com",
"email":"[email protected]"
}""";
System.out.println(myJson1);
JDK14中二次预览特性
JDK14的版本主要增加了两个escape sequences,分别是 \
与\s escape sequence
。
举例:
public class Feature05 {
//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);
}
}