-
Notifications
You must be signed in to change notification settings - Fork 35
JEP 378- Text Blocks enhancements added #322
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
AloisSeckar
merged 3 commits into
AloisSeckar:master
from
SanjanaMahapatra:feature/JEP378
Oct 29, 2025
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/main/java/org/javademos/java13/jep355/TextBlockFirstPreviewDemo.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package org.javademos.java13.jep355; | ||
|
|
||
| import org.javademos.commons.IDemo; | ||
|
|
||
| /// Demo for JDK 13 feature <strong>JEP 355 - Text Blocks (Preview)</strong>. | ||
| /// | ||
| /// Relates to: | ||
| /// - [JEP 326: Raw String Literals (Preview)](https://openjdk.org/jeps/326) | ||
| /// | ||
| /// This was a preview JEP. The feature is finalized in JDK 15 by JEP 378. | ||
| /// @see org.javademos.java15.jep378.TextBlockDemo | ||
| /// | ||
| /// @author SanjanaMahapatra | ||
|
|
||
|
|
||
| public class TextBlockFirstPreviewDemo implements IDemo { | ||
| @Override | ||
| public void demo() { | ||
| info(355); | ||
| } | ||
|
|
||
| } |
20 changes: 20 additions & 0 deletions
20
src/main/java/org/javademos/java14/jep368/TextBlockSecondPreviewDemo.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package org.javademos.java14.jep368; | ||
|
|
||
| import org.javademos.commons.IDemo; | ||
|
|
||
| /// Demo for JDK 14 feature <strong>JEP 368 - Text Blocks (Second Preview)</strong>. | ||
| /// | ||
| /// This was a preview JEP. The feature is finalized in JDK 15 by JEP 378. | ||
| /// @see org.javademos.java15.jep378.TextBlockDemo | ||
| /// | ||
| /// @author SanjanaMahapatra | ||
|
|
||
|
|
||
| public class TextBlockSecondPreviewDemo implements IDemo { | ||
|
|
||
| @Override | ||
| public void demo() { | ||
| info(368); | ||
| } | ||
|
|
||
| } |
118 changes: 118 additions & 0 deletions
118
src/main/java/org/javademos/java15/jep378/TextBlockDemo.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| package org.javademos.java15.jep378; | ||
|
|
||
| import org.javademos.commons.IDemo; | ||
|
|
||
| /// Demo for JDK 15 feature JEP 378 - Text Blocks. | ||
| /// | ||
| /// | ||
| /// JEP history: | ||
| /// - JDK 15: [JEP 378 - Text Blocks](https://openjdk.org/jeps/378) | ||
| /// - JDK 14: [JEP 368 - Text Blocks (Second Preview)](https://openjdk.org/jeps/368) | ||
| /// - JDK 13: [JEP 355 - Text Blocks (Preview)](https://openjdk.org/jeps/355) | ||
| /// | ||
| /// Further Reading: | ||
| /// - [Java Text Blocks](https://www.baeldung.com/java-text-blocks) | ||
| /// - [Programmer's Guide to Text Blocks](http://docs.oracle.com/javase/jp/15/text-blocks/index.html) | ||
| /// | ||
| /// @author alois.seckar@gmail.com, SanjanaMahapatra | ||
|
|
||
| public class TextBlockDemo implements IDemo{ | ||
|
|
||
| @Override | ||
| public void demo() { | ||
| info(378); | ||
|
|
||
| // Java < 15 | ||
| // if you want multi-line text, you have to chain it with "+" | ||
| // and include \n to split the output into lines | ||
| String oldString = "This is multiline string \n" | ||
| + "constructed in the old fashioned way \n" | ||
| + "divided to more lines with + symbols \n" | ||
| + "and with \\n to add line breaks \n"; | ||
|
|
||
| System.out.println("Old string for JDK < 15 "); | ||
| System.out.println(oldString); | ||
|
|
||
| // Based on the feedback received from JEP355, in the second preview JEP368, two new escape | ||
| // sequence for explicit control were introduced -> | ||
| // a) \s (A single space preventing automatic stripping of trailing whitespace. | ||
| // b) \ (A backslash at the end of a line to suppress the insertion of a new line character. | ||
|
|
||
| // This change introduced in JEP368, was also taken in the final implementation of JEP378. | ||
|
|
||
| // Example of New Escape | ||
|
|
||
| System.out.println(""" | ||
| The character literals and traditional string literals don't | ||
| allow the embedding of newlines, the \\<line-terminator> escape | ||
| sequence applicable for text blocks only. | ||
| """); | ||
|
|
||
| String rawLiteral = "Lorem ipsum dolor sit amet, consectetur adipiscing " + | ||
| "elit, sed do eiusmod tempor incididunt ut labore " + | ||
| "et dolore magna aliqua."; | ||
|
|
||
| System.out.println(rawLiteral); | ||
|
|
||
| System.out.println("The following example demonstrates having a newline, but actually does not do"); | ||
|
|
||
| String lineTerminatorText = """ | ||
| Lorem ipsum dolor sit amet, consectetur adipiscing \ | ||
| elit, sed do eiusmod tempor incididunt ut labore \ | ||
| et dolore magna aliqua.\ | ||
| """; | ||
|
|
||
| System.out.println(lineTerminatorText); | ||
|
|
||
| // Java 15+ | ||
| // you can have multi-line text inside """ and """ marks | ||
| // new lines are automatically being taken from new lines in source | ||
| String newString = """ | ||
| This is also multiline string | ||
| but constructed in the new way | ||
| divided to more lines without + symbols | ||
| and with no need to declare \\n line breaks | ||
| """; | ||
| System.out.println(); | ||
| System.out.println("New String for JDK 15+ "); | ||
| System.out.println(newString); | ||
|
|
||
| // you can refer to output to see both Strings print out equivalently | ||
|
|
||
| // adding some additional examples to support the statement | ||
|
|
||
| System.out.println("Text blocks can be also used as the method argument -"); | ||
| System.out.println(""" | ||
| This is the first line in statement | ||
| This is the second line in statement | ||
| This is the third line in statement | ||
| """ | ||
| ); | ||
|
|
||
| System.out.println(""" | ||
| When the text block contains sequence of three or more double quotes, | ||
| escape the first double quote of every run of three double quotes. | ||
| """); | ||
|
|
||
| String originalString = """ | ||
| String source = \"\"\" | ||
| String message = "Hello, World!"; | ||
| System.out.println(message); | ||
| \"\"\"; | ||
| """; | ||
|
|
||
| System.out.println("original string - "); | ||
| System.out.println(originalString); | ||
|
|
||
| String betterString = """ | ||
| String source = \""" | ||
| String message = "Hello, World!"; | ||
| System.out.println(message); | ||
| \"""; | ||
| """; | ||
|
|
||
| System.out.println("better string using text blocks - "); | ||
| System.out.println(betterString); | ||
|
|
||
| } | ||
| } |
45 changes: 0 additions & 45 deletions
45
src/main/java/org/javademos/java17/textblock/TextBlockDemo.java
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.