1. Core special characters and escaping
In programming, strings consist not only of letters but also of special characters: quotes, line breaks, tabs, and sometimes even markers like \n and \t. But if you want to write Hello, "user"! inside a string, the compiler will immediately wonder: “Are the quotes inside the string an error?” That’s where escaping comes in.
In Java, escaping is a way to “give special treatment” to certain characters inside a string. For this, the backslash \ (aka “backslash”) is used.
Classic escape sequences:
| Sequence | Description | Demo |
|---|---|---|
|
Line break | Pressed Enter |
|
Tab character | Tab |
|
Literal “backslash” | |
|
A quote inside a string | |
|
Single quote (rarely needed in strings) | |
|
Carriage return (old-style line break, rarely used) | |
|
Null character | (Invisible) |
|
Backspace (deletes the previous character — doesn’t always work in consoles) |
How it works
Let’s write a couple of lines with escaped characters to see them in action.
System.out.println("Hello, \"User\"!");
// Output: Hello, "User"!
System.out.println("C:\\Program Files\\MyApp");
// Output: C:\Program Files\MyApp
System.out.println("Line 1\nLine 2");
// Output:
// Line 1
// Line 2
System.out.println("I\tlove\ttabs!");
// Output:
// I love tabs!
With that single little stroke (\), you can “smuggle” almost any special character into a string without alarming the compiler or your teammate.
2. Multiline strings and line breaks
Very often you need to write a multiline message or ASCII art. The most straightforward way is to use \n in a regular string.
System.out.println("First line\nSecond line\nThird line");
However, this isn’t always convenient, so Java introduced text blocks. They start with triple quotes """ and let you write multiline text without escaping while preserving formatting — perfect for JSON, SQL, and HTML.
// Multiline literal (Text Block)
String json = """
{
"name": "Alice",
"age": 30,
"skills": ["Java", "SQL", "Cloud"]
}
""";
System.out.println(json);
"""
With text blocks you can also easily create multiline text that includes quotes without duplicating them.
3. Unicode — how Java stores characters from around the world
In today’s world we work not only with the Latin alphabet but also with Cyrillic, ideographs, mathematical symbols, and even emoji. All this is possible thanks to the character encoding system — Unicode.
What is Unicode?
Unicode is an international standard that assigns a unique number (code point) to each character, regardless of language or platform. Thanks to this, you can easily use Cyrillic text, English, Chinese characters, and even rare special symbols in Java at the same time.
Example string in different languages:
System.out.println("Hello, world! 你好! مرحبا!");
Unicode characters in strings
You can directly write any characters supported by your encoding or use escape sequences of the form \uXXXX, where XXXX is a 4-digit hexadecimal code of the character.
System.out.println("Character: \u263A"); // Will print 😊
For characters with a code greater than 65535 (for example, many emojis), a surrogate pair (UTF-16) is used:
// The character 😊 (code 1F60A) is represented by two char
System.out.println("Emoji: \uD83D\uDE0A"); // Will print 😊
What does this give you?
- You can store and process any text.
- Don’t be afraid to mix languages and use nonstandard symbols — Java supports Unicode out of the box.
- Especially important when working with international users and nonstandard alphabets.
4. Emojis and special symbols in strings
Modern strings are not just letters but also emoji! For example, you may want to send a cheerful message with a “smiley.”
How do you add an emoji to a string?
Insert the emoji directly into the string:
System.out.println("Hello! 😊");
Use Unicode codes: for most emojis you can use surrogate pairs.
System.out.println("Here is a cat: \uD83D\uDC31"); // 🐱
Emojis may take two characters in a string (due to UTF-16). Therefore, some operations (for example, getting the length) can surprise you:
String s = "😊";
System.out.println(s.length()); // Prints 2, not 1!
This happens because a single Unicode code point is encoded by two char in Java. For correct handling, use code points and the corresponding methods instead of a simple length().
5. Pitfalls and common mistakes
Error No. 1: Incorrect use of quotes inside a string. If you don’t escape inner quotes, the compiler “thinks” the string has ended.
System.out.println("He said: "Hello!""); // Error!
Correct:
System.out.println("He said: \"Hello!\"");
Error No. 2: A single backslash in a path. In Java, \ is used for special characters (\n, \t, \f, etc.). Therefore, the string "C:\Temp\file.txt" is interpreted incorrectly.
System.out.println("C:\Temp\file.txt"); // Error or unexpected result
Correct:
System.out.println("C:\\Temp\\file.txt"); // Double backslashes
Error No. 3: Ignoring text blocks. Starting with Java 15, use text blocks — they make working with multiline strings easier.
String path = """
C:\Temp\file.txt
""";
System.out.println(path);
Key rule: always escape special characters and quotes. This will save you from hidden bugs and hours of debugging.
GO TO FULL VERSION