I just tried the new text block feature in Java 13 and encountered a small issue.
I have read this article from Jaxcenter.
The closing triple quotation marks will affect the format.
String query = """
select firstName,
lastName,
email
from User
where id= ?
""";
System.out.println("SQL or JPL like query string :\n" + query);
This above format works well. To align with the the closing delimiter ("""), the multiline string left spaces before every lines.
But when I tried to compare the following two text block string, they are same format in the output console, but they are not equals, even after stripIntent
.
String hello = """
Hello,
Java 13
""";
String hello2 = """
Hello,
Java 13
""";
System.out.println("Hello1:\n" + hello);
System.out.println("Hello2:\n" + hello);
System.out.println("hello is equals hello2:" + hello.equals(hello2));
System.out.println("hello is equals hello2 after stripIndent():" + hello.stripIndent().equals(hello2.stripIndent()));
The output console is like:
hello is equals hello2:false
hello is equals hello2 after stripIndent():false
I am not sure where is wrong, or this is a text block design purpose?
Update: Just print hello2 stripIntent,
System.out.println("hello2 after stripIntent():\n" + hello2.stripIndent());
The whitespaces before every lines are NOT removed by stripIntent
as expected.
Updated: After read the related java doc, I think after the text block is compiled, it should has stripped the left intents of the lines in the block. What is the purpose of stripIntent
for text block? I know it is easy to understand when use it on a normal string.
The complete code is here.
TLDR. Your example strings are not equal and it is correct that Java tells you that they are not equal.
Consider reading a description of the
String.stripIndent
method. Here is a paraphrase from a jaxenter.com post:Note the words "that all lines have in common".
Now, apply "that all lines have in common" to the following literal string:
The key take away is "0 != 3".
There is a concept of incidental white space.
Your assumption that
equals
is inaccurate since those are essential white spaces and they will not be removed by either the compiler or
String#stripIndent
.To make it clear, let's keep representing an incidental white space as a dot.
Let's print them.
Let's call
String#stripIndent
on both and print the results.To understand why nothing has changed, we need to look into the documentation.
For both
String
s, the minimum indentation is0
.String#stripIndent
gives developers access to a Java version of the re-indentation algorithm used by the compiler.Testing with
jshell
:results in
note: no spaces at all
results in
Note that both results do NOT have spaces in the last line, after the last
\n
, sostripIndent()
does not strip any spacesstripIndent()
does the same as the compiler does with text blocks. Exampleresults in
that is, two spaces removed from all 3 lines; two spaces since the last line has 2 spaces (the other lines have more, so at most 2 spaces can be removed from all lines)