Home > Articles

  • Print
  • + Share This

String Literals

The char primitive supports variables that represent a single character. Sequences of characters are not represented by a primitive type in Java, but by a reference type, the String class, instead. This class is discussed in Chapter 8, "Using Strings and Text," but you need to know a little about strings now to finish the discussion of literals. String literals contain zero or more characters enclosed in double quotation marks. These characters can include the escape sequences listed in the "Character Literals" section earlier in this chapter. Both double quotation marks must appear on the same line of the source code, so strings cannot directly contain a new line character. To achieve the new line effect, you must use an escape sequence such as \n within the string. The double-quotation mark (") and backslash (\) characters must also be represented using escape sequences (\" and \\) within a string literal.

Some examples of string literals include

 "Hello World!\n"
 "The Devanagari numeral for 9 is \u096f "
  • + Share This
  • 🔖 Save To Your Account

Related Resources

There are currently no related titles. Please check back later.