The String Literal

Young businesswoman working in an office
Lindsay Upson/Image Source/Getty Images

A

String
literal is a sequence of characters used by Java programmers to populate
String

"I live at 22b Baker Street!"

is a

String

Although in your Java code you will be writing the text within the quotes, the Java compiler will interpret the characters as Unicode code points.

Unicode is a standard that assigns all letters, numbers, and symbols a unique numerical code. This means that every computer will display the same character for each numerical code. This means that if you know the number values you can actually write

String

"\u0049\u0020\u006C\u0069\u0076\u0065\u0020\u0061\u0074\u0020\u0032\u0032\u0042\u0020\u0042\u0061\u006B\u0065\u0072\u0020\u0053\u0074\u0072\u0065\u0065\u0074\u0021"

represents the same

String

Unicode and normal text characters can also be mixed. This is useful for characters you might not know how to type. For example, a character with an umlaut (e.g., Ä, Ö) as in "Thomas Müller plays for Germany." would be:

"Thomas M\u00FCller plays for Germany."

To assign a

String
object a value just use a
String

String text = "So does Dr Watson";

Escape Sequences

There are certain characters that you might want to include into a

String
literal which need to be identified to the compiler. Otherwise, it might get confused and not know what the
String
value is supposed to be. For example, imagine you want to put a quotation mark within a
String

"So my friend said, "It's how big?""

This would confuse the compiler because it expects all

String

\"

So the

String

"So my friend said, \"It's how big?\""

Now the compiler will come to the backslash and know the quotation mark is part of the

String
literal instead of its end point. If you're thinking ahead you're probably wondering but what if I want to have a backslash in my
String

\\

Some of the escape sequences available don't actually print a character to the screen. There are times when you might want to display some text split by a newline. For example:

The first line.
The second line.

This can be done by using the escape sequence for the newline character:

"The first line.\nThe second line."

It's a useful way to put a little bit of formatting into one

Sting

There are several useful escape sequences worth knowing:

  • \t
    is for inserting tabs into the literal
  • \b
    inserts a backspace
  • \n
    inserts a newline
  • \r
    inserts a carriage return
  • \'
    inserts a single quotation mark
  • \"
    inserts a double quotation mark
  • \\
    inserts a backslash

Example Java code can be found in the Fun With Strings Example Code.

Format
mla apa chicago
Your Citation
Leahy, Paul. "The String Literal." ThoughtCo, Apr. 5, 2023, thoughtco.com/the-string-literal-2034316. Leahy, Paul. (2023, April 5). The String Literal. Retrieved from https://www.thoughtco.com/the-string-literal-2034316 Leahy, Paul. "The String Literal." ThoughtCo. https://www.thoughtco.com/the-string-literal-2034316 (accessed March 19, 2024).