From 1acef4afd4b6eacb2f0025844fb7f1b2aaba7414 Mon Sep 17 00:00:00 2001 From: Sanjana Mahapatra Date: Wed, 29 Oct 2025 14:02:29 +0530 Subject: [PATCH 1/2] JEP 378- Text Blocks enhancements added --- src/main/java/org/javademos/init/JEPInfo.java | 2 +- .../org/javademos/init/Java13DemoLoader.java | 2 + .../org/javademos/init/Java14DemoLoader.java | 2 + .../org/javademos/init/Java15DemoLoader.java | 2 + .../jep355/TextBlockFirstPreviewDemo.java | 20 +++++ .../jep368/TextBlockSecondPreviewDemo.java | 46 ++++++++++ .../java15/jep378/TextBlockDemo.java | 86 +++++++++++++++++++ .../java17/textblock/TextBlockDemo.java | 45 ---------- src/main/resources/JDK13Info.json | 8 ++ src/main/resources/JDK14Info.json | 8 ++ src/main/resources/JDK15Info.json | 8 ++ 11 files changed, 183 insertions(+), 46 deletions(-) create mode 100644 src/main/java/org/javademos/java13/jep355/TextBlockFirstPreviewDemo.java create mode 100644 src/main/java/org/javademos/java14/jep368/TextBlockSecondPreviewDemo.java create mode 100644 src/main/java/org/javademos/java15/jep378/TextBlockDemo.java delete mode 100644 src/main/java/org/javademos/java17/textblock/TextBlockDemo.java diff --git a/src/main/java/org/javademos/init/JEPInfo.java b/src/main/java/org/javademos/init/JEPInfo.java index 836dd839..c1ceb6ce 100644 --- a/src/main/java/org/javademos/init/JEPInfo.java +++ b/src/main/java/org/javademos/init/JEPInfo.java @@ -20,7 +20,7 @@ public record JEPData(int jep, int jdk, String name, String dscr, boolean link, private static Map getAllInfos() { var jeps = new HashMap(); - var sources = Arrays.asList(14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25); + var sources = Arrays.asList(13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25); for (Integer src : sources) { var srcFile = MessageFormat.format("/JDK{0}Info.json", src); try (InputStream inputStream = org.javademos.init.JEPInfo.class.getResourceAsStream(srcFile)) { diff --git a/src/main/java/org/javademos/init/Java13DemoLoader.java b/src/main/java/org/javademos/init/Java13DemoLoader.java index babe0cbf..45060963 100644 --- a/src/main/java/org/javademos/init/Java13DemoLoader.java +++ b/src/main/java/org/javademos/init/Java13DemoLoader.java @@ -5,6 +5,7 @@ import org.javademos.commons.IDemo; import org.javademos.commons.IDemoLoader; import org.javademos.java13.jep354.SwitchExpressionsSecondPreview; +import org.javademos.java13.jep355.TextBlockFirstPreviewDemo; /** * Loads demos for Java 13. @@ -15,5 +16,6 @@ public class Java13DemoLoader implements IDemoLoader { public void loadDemos(Map demos) { // JEP 354 demos.put(354, new SwitchExpressionsSecondPreview()); + demos.put(355, new TextBlockFirstPreviewDemo()); } } diff --git a/src/main/java/org/javademos/init/Java14DemoLoader.java b/src/main/java/org/javademos/init/Java14DemoLoader.java index 731e96e8..0e03c6ec 100644 --- a/src/main/java/org/javademos/init/Java14DemoLoader.java +++ b/src/main/java/org/javademos/init/Java14DemoLoader.java @@ -7,6 +7,7 @@ import org.javademos.java14.jep305.InstanceofPatternMatchingPreview; import org.javademos.java14.jep359.RecordsPreviewDemo; import org.javademos.java14.jep361.SwitchExpressionsDemo; +import org.javademos.java14.jep368.TextBlockSecondPreviewDemo; import org.javademos.java14.jep370.ForeignMemoryAccessDemo; /** @@ -19,6 +20,7 @@ public void loadDemos(Map demos) { demos.put(305, new InstanceofPatternMatchingPreview()); demos.put(359, new RecordsPreviewDemo()); demos.put(361, new SwitchExpressionsDemo()); + demos.put(368, new TextBlockSecondPreviewDemo()); demos.put(370, new ForeignMemoryAccessDemo()); } } diff --git a/src/main/java/org/javademos/init/Java15DemoLoader.java b/src/main/java/org/javademos/init/Java15DemoLoader.java index 1ff1bd02..bda8b3e3 100644 --- a/src/main/java/org/javademos/init/Java15DemoLoader.java +++ b/src/main/java/org/javademos/init/Java15DemoLoader.java @@ -9,6 +9,7 @@ import org.javademos.java15.jep373.DatagramSocketDemo; import org.javademos.java15.jep375.InstanceofPatternMatchingSecondPreview; import org.javademos.java15.jep377.ZGarbageCollectorDemo; +import org.javademos.java15.jep378.TextBlockDemo; import org.javademos.java15.jep381.SolarisSparcRemovalDemo; import org.javademos.java15.jep383.ForeignMemoryAccessDemo; import org.javademos.java15.jep384.RecordsSecondPreviewDemo; @@ -29,6 +30,7 @@ public void loadDemos(Map demos) { demos.put(373, new DatagramSocketDemo()); demos.put(375, new InstanceofPatternMatchingSecondPreview()); demos.put(377, new ZGarbageCollectorDemo()); + demos.put(378, new TextBlockDemo()); demos.put(381, new SolarisSparcRemovalDemo()); demos.put(383, new ForeignMemoryAccessDemo()); demos.put(384, new RecordsSecondPreviewDemo()); diff --git a/src/main/java/org/javademos/java13/jep355/TextBlockFirstPreviewDemo.java b/src/main/java/org/javademos/java13/jep355/TextBlockFirstPreviewDemo.java new file mode 100644 index 00000000..92ec661d --- /dev/null +++ b/src/main/java/org/javademos/java13/jep355/TextBlockFirstPreviewDemo.java @@ -0,0 +1,20 @@ +package org.javademos.java13.jep355; + +import org.javademos.commons.IDemo; + +/// Demo for JDK 13 feature JEP 355 - Text Blocks (Preview). +/// +/// 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);} + +} diff --git a/src/main/java/org/javademos/java14/jep368/TextBlockSecondPreviewDemo.java b/src/main/java/org/javademos/java14/jep368/TextBlockSecondPreviewDemo.java new file mode 100644 index 00000000..60267af0 --- /dev/null +++ b/src/main/java/org/javademos/java14/jep368/TextBlockSecondPreviewDemo.java @@ -0,0 +1,46 @@ +package org.javademos.java14.jep368; + +import org.javademos.commons.IDemo; + +/// Demo for JDK 14 feature JEP 368 - Text Blocks (Second Preview). +/// +/// 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); + + // example supporting the claim + + // Example of New Escape + + System.out.println(""" + The character literals and traditional string literals don't + allow the embedding of newlines, the \\ 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); + } + +} diff --git a/src/main/java/org/javademos/java15/jep378/TextBlockDemo.java b/src/main/java/org/javademos/java15/jep378/TextBlockDemo.java new file mode 100644 index 00000000..a56513f5 --- /dev/null +++ b/src/main/java/org/javademos/java15/jep378/TextBlockDemo.java @@ -0,0 +1,86 @@ +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); + + // 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("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); + + } +} diff --git a/src/main/java/org/javademos/java17/textblock/TextBlockDemo.java b/src/main/java/org/javademos/java17/textblock/TextBlockDemo.java deleted file mode 100644 index 7ea9c02a..00000000 --- a/src/main/java/org/javademos/java17/textblock/TextBlockDemo.java +++ /dev/null @@ -1,45 +0,0 @@ -package org.javademos.java17.textblock; - -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) -/// -/// @author alois.seckar@gmail.com -public class TextBlockDemo implements IDemo { - - @Override - public void demo() { - info("TEXT BLOCK DEMO", "Examples for 'text blocks' feature\nintroduced in Java 13 and finalized in Java 15"); - - // 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(oldString); - - // 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(newString); - - // you can refer to output to see both Strings print out equivalently - } - -} diff --git a/src/main/resources/JDK13Info.json b/src/main/resources/JDK13Info.json index aaf4a842..4fb0ca84 100644 --- a/src/main/resources/JDK13Info.json +++ b/src/main/resources/JDK13Info.json @@ -6,5 +6,13 @@ "dscr": "Second preview of switch expressions introducing yield statement. Finalized in JEP 361 (JDK 14).", "link": true, "code": false + }, + { + "jep": 355, + "jdk": 13, + "name": "JEP 355 - Text Blocks (Preview)", + "dscr": "Up-to-date demo moved to `org.javademos.java15.jep378.TextBlockDemo` (JEP 378)", + "link": true, + "code": false } ] diff --git a/src/main/resources/JDK14Info.json b/src/main/resources/JDK14Info.json index d0ef0e39..8828f466 100644 --- a/src/main/resources/JDK14Info.json +++ b/src/main/resources/JDK14Info.json @@ -23,6 +23,14 @@ "link": false, "code": true }, + { + "jep": 368, + "jdk": 14, + "name": "JEP 368 - Text Blocks (Second Preview)", + "dscr": "Up-to-date demo moved to `org.javademos.java15.jep378.TextBlockDemo` (JEP 378)", + "link": true, + "code": false + }, { "jep": 370, "jdk": 14, diff --git a/src/main/resources/JDK15Info.json b/src/main/resources/JDK15Info.json index 535b22e7..225d2781 100644 --- a/src/main/resources/JDK15Info.json +++ b/src/main/resources/JDK15Info.json @@ -55,6 +55,14 @@ "link": false, "code": false }, + { + "jep": 378, + "jdk": 15, + "name": "JEP 378 - Text Blocks", + "dscr": "Demonstrates adding text blocks to the Java language; see TextBlockDemo.java", + "link": false, + "code": true + }, { "jep": 381, "jdk": 15, From 3583427a822a8b50b3748b8c4723eeaa2121a963 Mon Sep 17 00:00:00 2001 From: Sanjana Mahapatra Date: Wed, 29 Oct 2025 21:52:12 +0530 Subject: [PATCH 2/2] changes done for JEP378 as per the review comments --- .../jep355/TextBlockFirstPreviewDemo.java | 4 ++- .../jep368/TextBlockSecondPreviewDemo.java | 26 --------------- .../java15/jep378/TextBlockDemo.java | 32 +++++++++++++++++++ 3 files changed, 35 insertions(+), 27 deletions(-) diff --git a/src/main/java/org/javademos/java13/jep355/TextBlockFirstPreviewDemo.java b/src/main/java/org/javademos/java13/jep355/TextBlockFirstPreviewDemo.java index 92ec661d..2e5231c9 100644 --- a/src/main/java/org/javademos/java13/jep355/TextBlockFirstPreviewDemo.java +++ b/src/main/java/org/javademos/java13/jep355/TextBlockFirstPreviewDemo.java @@ -15,6 +15,8 @@ public class TextBlockFirstPreviewDemo implements IDemo { @Override - public void demo() {info(355);} + public void demo() { + info(355); + } } diff --git a/src/main/java/org/javademos/java14/jep368/TextBlockSecondPreviewDemo.java b/src/main/java/org/javademos/java14/jep368/TextBlockSecondPreviewDemo.java index 60267af0..52ba6c6d 100644 --- a/src/main/java/org/javademos/java14/jep368/TextBlockSecondPreviewDemo.java +++ b/src/main/java/org/javademos/java14/jep368/TextBlockSecondPreviewDemo.java @@ -15,32 +15,6 @@ public class TextBlockSecondPreviewDemo implements IDemo { @Override public void demo() { info(368); - - // example supporting the claim - - // Example of New Escape - - System.out.println(""" - The character literals and traditional string literals don't - allow the embedding of newlines, the \\ 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); } } diff --git a/src/main/java/org/javademos/java15/jep378/TextBlockDemo.java b/src/main/java/org/javademos/java15/jep378/TextBlockDemo.java index a56513f5..eb9cc63b 100644 --- a/src/main/java/org/javademos/java15/jep378/TextBlockDemo.java +++ b/src/main/java/org/javademos/java15/jep378/TextBlockDemo.java @@ -33,6 +33,37 @@ public void demo() { 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 \\ 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 @@ -42,6 +73,7 @@ public void demo() { 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);