JavaWorking with Strings and Special Characters in Java

Working with Strings and Special Characters in Java content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Whether you create a String using the constructor or as a string literal, the most important thing to remember is that the entire sequence must be enclosed in quotation marks. This can pose a problem if the string itself must contain quotation marks. That is where character escaping comes in. Besides quotation marks, you can insert several special characters into a string in the same way. We will be looking at character escaping, escape characters, and special characters in this Java programming tutorial.

Read: Top Java Online Training Courses and Bundles

Character Escaping in Java

In Java, as in many other programming languages, character escaping is accomplished using the “backslash” () symbol. In Java, a backslash, combined with a character to be “escaped”, is called a control sequence. For example, is a control sequence for displaying quotation marks in a String. The control sequence tells the compiler that the quotation mark should be included as part of the String. Here is some example code that prints my favorite movie to the screen showing how to use escape characters in Java:

public class DoubleQuotesExample {

  public static void main(String[] args) {
    String myFavoriteMovie = "My favorite movie is "Raiders of the Lost Ark".";
    // Outputs My favorite movie is "Raiders of the Lost Ark".

Special Characters in Java

Some characters, like the quotation mark, are part of the Java language; others, like the new line character, are simply a regular letter – the letter n in this case. However, when combined with the backslash symbol, these special characters tell the compiler to treat them differently than it usually would. When the compiler encounters n in the text, it understands that this is not just a symbol and a letter to display in the console, but rather, a special command to start a new line – equivalent to pressing Enter. For example, this example Java code shows how to use the newline special character n to to display the lyrics of a song:

public class NewLineExample {

  public static void main(String[] args) {
    String lyric = "Wave after wave will flow with the tidenAnd bury the world as it doesnTide after tide will flow and recedenLeaving life to go on as it was);

The code above outputs our lyrics as four distinct lines:

Wave after wave will flow with the tide
And bury the world as it does
Tide after tide will flow and recede
Leaving life to go on as it was

Escaping Backslashes in Java

Imagine that we are working with a file path like the following:

String reportPath = "I:My Documentsquarterly reportsQ1.rpt";

Since the compiler does not recognize as anything other than a control sequence, it expects the backslash to be followed by a special character to be interpreted in a certain way. However, in this case, is followed by ordinary letters. The solution? Exactly the same as before: we just escape the character!

public class BackslashExample {

  public static void main(String[] args) {
    String reportPath = "I:\My Documents\quarterly reports\Q1.rpt";
    // Outputs I:My Documentsquarterly reportsQ1.rpt

Here is the full list of special characters in Java:

  • t – tab
  • b – backspace (a step backward in the text or deletion of a single character)
  • n – new line
  • r – carriage return
  • f – form feed
  • – single quote
  • – double quote
  • \ – backslash

The code example below shows how to use several of these special characters in Java:

public class SpecialCharactersExample {

  public static void main(String[] args) {
    String strWithTabs = "Header1tHeader2tHeader3"; 
    // Outputs Header1    Header2    Header3

    String strWithNewLines = "As I walk,nmy life driftsnbefore me";
    /* Outputs:
    As I walk,
    my life drifts
    before me
    String strWithBackslash = "And\Or"; 
    // Outputs AndOr
    String strWithCarriageReturn = "CarriagerReturn"; 
    /* Outputs:
    String strWithSingleQuote = "Other people's money"; 
    // Outputs Other people's money

    String strWithDoubleQuotes = "Dwayne "The Rock" Johnson"; 
    // Outputs Dwayne "The Rock" Johnson

Read: Formatting Strings in Java: String.format() Method

Escaping Unicode Characters in Java

Unicode is a standard character encoding that includes the symbols of practically every written language in the world. All Unicode character codes have the form “u+<hexadecimal digit>“. For example, the copyright symbol (©) is represented by u00A9. To include this character in a Java String, you need to escape it, as seen in the following code example:

public class CopyrightExample {

  public static void main(String[] args) {
    System.out.println(""Star Wars", u00A9 1977 All Rights Reserved");
    // Outputs "Star Wars", © 1977 All Rights Reserved

You can use Unicode to display special characters and text written in a multitude of different languages. Here is a program that provides just a glimpse at what is possible:

public class UnicodeCharactersExample {
  public static void main(String[] args) {
    char forwardSlash = 'u002F';
    // Outputs /
    char questionMark = 'u003F';
    // Outputs ?
    char number1 = 'u0031';
    // Outputs 1
    char tilde = 'u007E';
    // Outputs ~
    char dollarSign = 'u0024';
    // Outputs $
    char lowercaseA = 'u0061';
    // Outputs a
    char uppercaseA = 'u0041';
    // Outputs A
    char japaneseYen = 'u00a5';
    // Outputs ¥
    char romanAeWithAcuteAccent = 'u01FC';
    // Outputs Ǽ 
    char greekCapitalAlpha = 'u0391';
    // Outputs Α
    char greekCapitalOmega = 'u03A9';
    // Outputs Ω

Final Thoughts on Special Characters in Java

In this programming tutorial, we delved into the topic of character escaping and special characters in Java, including the encoding of Unicode characters. Speaking of which, Unicode is a fairly expansive subject. If you are interested in exploring Unicode in more detail, there is plenty of good information to be found in the Oracle Java docs.

Read more Java programming tutorials and guides to software development.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories