A string literal is a sequence of Unicode characters inside of a pair of double quotes. The javac compiler translates all characters in a string literal into Unicode characters as the bytecode file (.class) is created. String literals can contain escape sequences for special characters. If the sentences above sound like technobabble, please review my tutorials on the Char Data Type, Hexadecimal Literals, and Escape Sequences.

  • String a = "This is how you surround a letter, word, or phrase with \"double quotes\" in a string literal.";
  • String b = "Dogs and Cats";
  • String c = "\u0044\u0070\u0067\u0073\u0020\u0061\u006E\u0064\u0020\u0043\u0061\u0074\u0073"; // Unicode Dogs and Cats
I am going to take a minute to talk about code pages and how they relate to Unicode characters. What is a code page? A code page is a table of values that describes the character set used for human-readable font characters and glyphs. Letters or symbols such as the euro € currency character where not part of the original character set for personal computers way back in the early days. There are many different types of code pages, you can Wiki "code page" for a more detailed explanation of how they work. For this tutorial, we need let the CMD window know that we would like to use a code page that will display the euro character properly. We will do this by using the chcp command. The chcp command is short for "change code page", we will be changing the code page to table 1250.

Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>md StringLiterals
C:\Java>cd StringLiterals
C:\Java\StringLiterals>Notepad StringLiterals.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.

class StringLiterals {
    public static void main(String args[]) {
        String a = "This is how you surround a letter, word, or phrase with \"double quotes\" in a string literal."; // \" double quote escape sequence
        String b = "Dogs and Cats";
        String c = "\u0044\u006F\u0067\u0073\u0020\u0061\u006E\u0064\u0020\u0043\u0061\u0074\u0073"; // Dogs and Cats in Unicode escape sequences
        String s = "The euro character: \u20AC"; // \u20AC Unicode escape sequence with the hexadecimal euro value


Now switch back to the command prompt (CMD) and type in javac StringLiterals.java and press Enter.
!Important! Now type in chcp 1250 and press Enter.
Now type in java StringLiterals and press Enter.

C:\Java\StringLiterals>javac StringLiterals.java
C:\Java\StringLiterals>chcp 1250
Active code page: 1250
C:\Java\StringLiterals>java StringLiterals
This is how you surround a letter, word, or phrase with "double quotes" in a string literal.
Dogs and Cats
Dogs and Cats
The euro character: €

Final thoughts

You will use string literals in almost every project that you create. Having and in-depth understanding of how string literals will make your life much easier in the future.