DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Writing DTOs With Java8, Lombok, and Java14+
  • Formatting Strings in Java: String.format() Method
  • Jakarta WebSocket Essentials: A Guide to Full-Duplex Communication in Java
  • Java Is Greener on Arm

Trending

  • A Complete Guide to Modern AI Developer Tools
  • Immutable Secrets Management: A Zero-Trust Approach to Sensitive Data in Containers
  • Orchestrating Microservices with Dapr: A Unified Approach
  • A Simple, Convenience Package for the Azure Cosmos DB Go SDK
  1. DZone
  2. Coding
  3. Java
  4. JDK 12: Raw String Literals

JDK 12: Raw String Literals

Want to check out the newest features coming to JDK 12? Click here to learn more about changes to raw string literals in JDK 12.

By 
Hemambara Vamsi Kotari user avatar
Hemambara Vamsi Kotari
·
Updated Nov. 14, 18 · News
Likes (9)
Comment
Save
Tweet
Share
29.2K Views

Join the DZone community and get the full member experience.

Join For Free

JEP 326 Raw string literals provide a new way to declare strings without escape characters or unicode escapes. Let's take a look at the example below that declares multi-line strings with JDK 12 and prior releases.

To print all the days in a week with new lines, here is how we do it with the current JDK 11 release:

 String daysInAWeekStr = "Days in a week \n" +
                                "\t\t\tSunday\n" +
                                 "\t\t\tMonday\n" +
                                "\t\t\tTuesday\n" +
                                "\t\t\tWednesday\n" +
                                 "\t\t\tThursday\n" +
                                 "\t\t\tFriday\n" +
                                 "\t\t\tSaturday\n";
System.out.println(daysInAWeekStr);


This is what it will look like in JDK 12:

String daysInAWeekStr = `Days in a week
                                Sunday
                                 Monday
                                 Tuesday
                                 Wednesday
                                 Thursday
                                 Friday
                                 Saturday`;

System.out.println(daysInAWeekStr);


Notice "`" characters to define the string variable. It avoids backslashes to escape unicode characters,\n new lines, and avoid "+" appending strings. It also increases the readability by avoiding leaning toothpick syndrome.

Output:

java --enable-preview <class>
Days in a week 
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday


This feature is now (as of writing this blog) available as a preview feature in JDK 12 early access builds. In order to enable this use, pass the --enable-preview argument.

For more JDK 12 features scheduled in March 2019, click here. This list is not complete and more features might be added. We have to wait until Dec 2018 for the exhaustive list.

Java Development Kit Java (programming language) Strings

Published at DZone with permission of Hemambara Vamsi Kotari. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Writing DTOs With Java8, Lombok, and Java14+
  • Formatting Strings in Java: String.format() Method
  • Jakarta WebSocket Essentials: A Guide to Full-Duplex Communication in Java
  • Java Is Greener on Arm

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!