Java Special Characters

Using escape sequences and special characters in Java strings

✨ What are Special Characters?

Special characters in Java are characters that have special meaning or cannot be typed directly. They use escape sequences starting with backslash (\) to represent formatting and control characters.


// Common special characters
String newLine = "First line\nSecond line";
String tab = "Name:\tJohn Doe";
String quote = "He said, \"Hello World!\"";

System.out.println(newLine);
System.out.println(tab);
System.out.println(quote);
                                    

Output:

First line
Second line

Name:    John Doe

He said, "Hello World!"

Types of Special Characters

📄

Formatting

Control text layout and spacing

String text = "Line 1\nLine 2\tTabbed";
// \n = new line, \t = tab
💬

Quotes

Include quotes within strings

String quote = "She said \"Hi!\"";
String path = "C:\\Users\\John";
🔤

Unicode

Special symbols and characters

String heart = "I \u2764 Java";
String copyright = "\u00A9 2023";
🎛️

Control

Special control characters

String backspace = "Hello\b\b World";
String carriageReturn = "Start\rNew";

🔹 Common Escape Sequences

Most frequently used special characters:

// Line formatting
String multiLine = "First line\nSecond line\nThird line";
String tabbed = "Name:\tJohn\nAge:\t25\nCity:\tNew York";

// Quotes and backslashes
String doubleQuote = "He said, \"Java is awesome!\"";
String singleQuote = "It's a beautiful day";
String backslash = "File path: C:\\Program Files\\Java";

// Carriage return and backspace
String overwrite = "Hello World\rJava";  // Java overwrites Hello
String correction = "Javva\b\ba";        // Corrects to Java

System.out.println("Multi-line text:");
System.out.println(multiLine);
System.out.println("\nTabbed format:");
System.out.println(tabbed);
System.out.println("\nQuotes: " + doubleQuote);
System.out.println("Path: " + backslash);

Output:

Multi-line text:
First line
Second line
Third line


Tabbed format:
Name:    John
Age:    25
City:    New York


Quotes: He said, "Java is awesome!"
Path: C:\Program Files\Java

🔹 Escape Sequence Reference

Complete list of Java escape sequences:

// All escape sequences with examples
System.out.println("\\n - New line:");
System.out.println("Line 1\nLine 2");

System.out.println("\n\\t - Tab:");
System.out.println("Column1\tColumn2\tColumn3");

System.out.println("\n\\r - Carriage return:");
System.out.println("Overwritten\rNew text");

System.out.println("\n\\b - Backspace:");
System.out.println("Mistakke\b\be");

System.out.println("\n\\f - Form feed:");
System.out.println("Page 1\fPage 2");

System.out.println("\n\\' - Single quote:");
System.out.println("It's working!");

System.out.println("\n\\\" - Double quote:");
System.out.println("She said \"Hello\"");

System.out.println("\n\\\\ - Backslash:");
System.out.println("Path: C:\\Users\\Documents");

Output:

\n - New line:
Line 1
Line 2


\t - Tab:
Column1    Column2    Column3


\r - Carriage return:
New text


\b - Backspace:
Mistake


\f - Form feed:
Page 1
Page 2


\' - Single quote:
It's working!


\" - Double quote:
She said "Hello"


\\ - Backslash:
Path: C:\Users\Documents

🔹 Unicode Characters

Using Unicode escape sequences for special symbols:

// Unicode escape format: \uXXXX (4 hex digits)
String symbols = "Heart: \u2764\n" +
                 "Star: \u2605\n" +
                 "Music: \u266B\n" +
                 "Copyright: \u00A9\n" +
                 "Trademark: \u2122\n" +
                 "Degree: 25\u00B0C\n" +
                 "Arrow: \u2192\n" +
                 "Check: \u2713";

// Currency symbols
String currencies = "Dollar: \u0024\n" +
                   "Euro: \u20AC\n" +
                   "Pound: \u00A3\n" +
                   "Yen: \u00A5";

// Mathematical symbols
String math = "Pi: \u03C0\n" +
             "Infinity: \u221E\n" +
             "Plus-minus: \u00B1\n" +
             "Not equal: \u2260";

System.out.println("Symbols:");
System.out.println(symbols);
System.out.println("\nCurrencies:");
System.out.println(currencies);
System.out.println("\nMath symbols:");
System.out.println(math);

Output:

Symbols:
Heart: ❤
Star: ★
Music: ♫
Copyright: ©
Trademark: ™
Degree: 25°C
Arrow: →
Check: ✓


Currencies:
Dollar: $
Euro: €
Pound: £
Yen: ¥


Math symbols:
Pi: π
Infinity: ∞
Plus-minus: ±
Not equal: ≠

🔹 Practical Examples

Real-world usage of special characters:

// Creating formatted output
String receipt = "========== RECEIPT ==========\n" +
                "Item\t\tPrice\tQty\tTotal\n" +
                "Laptop\t\t$999.99\t1\t$999.99\n" +
                "Mouse\t\t$25.50\t2\t$51.00\n" +
                "=============================\n" +
                "Total:\t\t\t\t$1050.99\n" +
                "Thank you for shopping! \u2764";

// File paths (Windows style)
String windowsPath = "C:\\Users\\John\\Documents\\MyFile.txt";
String javaPath = "src\\main\\java\\com\\example\\Main.java";

// JSON-like string with quotes
String jsonData = "{\n" +
                 "\t\"name\": \"John Doe\",\n" +
                 "\t\"age\": 30,\n" +
                 "\t\"city\": \"New York\"\n" +
                 "}";

// Dialog with quotes
String conversation = "Alice: \"How are you today?\"\n" +
                     "Bob: \"I'm doing great, thanks!\"\n" +
                     "Alice: \"That's wonderful to hear.\"";

System.out.println(receipt);
System.out.println("\nWindows path: " + windowsPath);
System.out.println("\nJSON data:");
System.out.println(jsonData);
System.out.println("\nConversation:");
System.out.println(conversation);

Output:

========== RECEIPT ==========
Item        Price    Qty    Total
Laptop      $999.99    1    $999.99
Mouse       $25.50    2    $51.00
=============================
Total:                $1050.99
Thank you for shopping! ❤


Windows path: C:\Users\John\Documents\MyFile.txt


JSON data:
{
"name": "John Doe",
"age": 30,
"city": "New York"
}


Conversation:
Alice: "How are you today?"
Bob: "I'm doing great, thanks!"
Alice: "That's wonderful to hear."

Tips for Using Special Characters:

  • Use \n for line breaks in console output
  • Use \t for consistent spacing in tables
  • Always escape \" when including quotes in strings
  • Use \\ for file paths on Windows
  • Unicode sequences work for emojis and special symbols

🧠 Test Your Knowledge

What does the escape sequence \t represent?