Special Characters and Strings in Java

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\".";
    System.out.println(myFavoriteMovie);
    // 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 tide\nAnd bury the world as it does\nTide after tide will flow and recede\nLeaving life to go on as it was);
    System.out.println(lyric);
  }
}

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 Documents\quarterly reports\Q1.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";
    System.out.println(reportPath);
    // Outputs I:\My Documents\quarterly reports\Q1.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 = "Header1\tHeader2\tHeader3"; 
    System.out.println(strWithTabs); 
    // Outputs Header1    Header2    Header3

    String strWithNewLines = "As I walk,\nmy life drifts\nbefore me";
    System.out.println(strWithNewLines); 
    /* Outputs:
    As I walk,
    my life drifts
    before me
    */
    
    String strWithBackslash = "And\\Or"; 
    System.out.println(strWithBackslash); 
    // Outputs And\Or
    
    String strWithCarriageReturn = "Carriage\rReturn"; 
    System.out.println(strWithCarriageReturn); 
    /* Outputs:
    Carriage
    Return
    */
    
    String strWithSingleQuote = "Other people\'s money"; 
    System.out.println(strWithSingleQuote); 
    // Outputs Other people's money

    String strWithDoubleQuotes = "Dwayne \"The Rock\" Johnson"; 
    System.out.println(strWithDoubleQuotes); 
    // 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";
    System.out.println(forwardSlash);
    // Outputs /
    
    char questionMark = '\u003F';
    System.out.println(questionMark);
    // Outputs ?
    
    char number1 = '\u0031';
    System.out.println(number1);
    // Outputs 1
    
    char tilde="\u007E";
    System.out.println(tilde);
    // Outputs ~
    
    char dollarSign = '\u0024';
    System.out.println(dollarSign);
    // Outputs $
    
    char lowercaseA = '\u0061';
    System.out.println(lowercaseA);
    // Outputs a
    
    char uppercaseA = '\u0041';
    System.out.println(uppercaseA);
    // Outputs A
    
    char japaneseYen = '\u00a5';
    System.out.println(japaneseYen);
    // Outputs ¥
    
    char romanAeWithAcuteAccent="\u01FC";
    System.out.println(romanAeWithAcuteAccent);
    // Outputs Ǽ 
    
    char greekCapitalAlpha="\u0391";
    System.out.println(greekCapitalAlpha);
    // Outputs Α
    
    char greekCapitalOmega="\u03A9";
    System.out.println(greekCapitalOmega);
    // 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.

Like this post? Please share to your friends:
Leave a Reply

;-) :| :x :twisted: :smile: :shock: :sad: :roll: :razz: :oops: :o :mrgreen: :lol: :idea: :grin: :evil: :cry: :cool: :arrow: :???: :?: :!: