CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > String > Java Special Characters: Escaping, Unicode, and Text Blocks

Java String Tutorial

  • Introduction to String
  • String Immutability
  • Common Methods
  • String Comparison
  • Formatting in String
  • Performance Optimization
  • String Manipulation
  • Java Regex Example
  • Special Characters in String
  • String Security
  • Java 8+ String Enhancements
  • Internationalization
  • Advanced Java String Techniques
  • Java String Best Practices

Java Special Characters: Escaping, Unicode, and Text Blocks

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Last updated: July 1, 2026 · 3 min read · 0 Comments
Share: in X

Table of Contents

  • 1. Escape Sequences in Java Strings
    • Common Escape Characters
  • 2. Unicode Characters
    • Using Unicode in Strings
  • 3. Text Blocks (Java 15+)
    • Traditional vs. Text Block
    • Text Block Rules
  • 4. Handling Edge Cases
    • Escaping in Text Blocks
    • Mixing Quotes
  • Common Mistakes
  • FAQ
    • How to write a Windows file path without escaping?
    • Can I use text blocks for short strings?
    • What Java versions support text blocks?

1. Escape Sequences in Java Strings

Java uses backslashes (\) to escape characters with special meanings.

Common Escape Characters

EscapeMeaningExampleOutput
\nNewline"Line1\nLine2"Line1
Line2
\tTab"Name:\tAlice"Name:   Alice
\"Double Quote"She said \"Hi\""She said “Hi”
\\Backslash"C:\\Users\\"C:\Users\
\'Single Quote'Don\'t stop'Don’t stop

Example:

String json = "{ \"name\": \"Alice\", \"age\": 30 }";  
System.out.println(json);  
// Output: { "name": "Alice", "age": 30 }  

2. Unicode Characters

Java supports Unicode via \u followed by a 4-digit hex code.

Using Unicode in Strings

String copyright = "Copyright \u00A9 2024";  // © symbol  
String heart = "I \u2764 Java!";             // ❤ symbol  

Key Notes:

  • Unicode works for any character, including emojis: \uD83D\uDE00 (😀).
  • Use hex values from Unicode charts.

3. Text Blocks (Java 15+)

Text blocks (""") simplify writing multi-line strings without escape characters.

Traditional vs. Text Block

// Old approach (messy):  
String html = "<html>\n" +  
              "  <body>\n" +  
              "    <p>Hello</p>\n" +  
              "  </body>\n" +  
              "</html>";  

// Text block (clean):  
String htmlBlock = """  
                  <html>  
                    <body>  
                      <p>Hello</p>  
                    </body>  
                  </html>  
                  """;  

Text Block Rules

  1. Start/end with """ on separate lines.
  2. Indentation is removed based on the closing """ position.
  3. Escape """ inside with \""".

4. Handling Edge Cases

Escaping in Text Blocks

Use \ to suppress line breaks or escape delimiters:

String poem = """  
              Roses are red,\  
              Violets are blue.\  
              Java is awesome,\  
              And so are you!  
              """;  
// Output: Roses are red, Violets are blue. Java is awesome, And so are you!  

Mixing Quotes

Text blocks handle quotes without escaping:

String sql = """  
             SELECT * FROM users  
             WHERE name = "Alice"  
             """;  

Common Mistakes

❌ Forgetting to Escape Backslashes:

String path = "C:\Users\file.txt";  // Error!  
String path = "C:\\Users\\file.txt";  // ✅  

❌ Incorrect Unicode Syntax:

String error = "\u00G1";  // Invalid hex 'G' → compile error  
String correct = "\u00F1";  // ñ  

❌ Text Block Alignment Issues:

// Misaligned closing """ adds unintended spaces:  
String s = """  
           Hello  
           """;  // "Hello\n" (no trailing space)  

FAQ

How to write a Windows file path without escaping?

Use text blocks (Java 15+) or replace \ with /:

String path = """
C:\Users\Alice\file.txt
"""; // Backslashes allowed in text blocks!

Can I use text blocks for short strings?

Yes, but they’re best for multi-line content (JSON/HTML).

What Java versions support text blocks?

Standardized in Java 15 (preview in 13 and 14).

Related Posts:

  • Install and Set Up Java Development Environment
  • Control Statements in Java
  • What Is Java Swing? A Complete Guide to Java’s GUI Toolkit
  • Identifiers in Java
  • Character data type in Java
  • MySQL Commands for Developers
Tags:javaString
Was this article helpful?
← Previous ArticleJava Regex Tutorial: Pattern, Matcher, and Practical Examples
Next Article →Java String Security: Handling Passwords, SQL, and User Input

Leave a Comment Cancel reply

You must be logged in to post a comment.

Recent Posts

  • How to implement Passwordless Authentication in Spring Boot: A Step-by-Step Guide
  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Linkedin

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
© 2026 CoderSathi. All rights reserved. Privacy Policy · Sitemap