Character Literals in Java
true and false are the only boolean literals, and they are written without quotes.
Two Words That Look Alike
Java programs contain many values written directly in the source code. These written values are called literals. Two of the most common are the word true and the text true inside double quotes. They look almost identical on the page, yet Java treats them as different kinds of things. Before reading further, try the question below and commit to an answer.
What do you think happens?
Is the unquoted word true and the quoted text "true" the same kind of value in Java?
Reveal answer
No, the unquoted word is a boolean literal and the quoted text is a String literal.
Java reads the spelling and punctuation of a literal to decide its type. Without quotes, true is a boolean literal. Inside double quotes, it is a String literal containing four characters.
Reading a Literal from Left to Right
A useful habit is to read a literal from left to right and notice three things: whether it starts with a quotation mark, which kind of quotation mark it uses, and whether any special characters appear inside it. A bare word with no quotes points to a boolean literal. A piece of text wrapped in double quotes points to a String literal. Text wrapped in single quotes belongs to a different category, the char literal. Each of these checks is a clue about what Java will do with the value.
- 1Look at the first character of the literal. Is it a double quote, a single quote, or a letter?
- 2If it is a bare word, check whether it is exactly true or exactly false.
- 3If it is wrapped in double quotes, it is a String literal. Check that both the opening and closing quotes are present.
- 4If it is wrapped in single quotes, it is a char literal. Check that the quotes match each other.
- 5Scan the inside of any quoted literal for backslashes and confirm each special character is written in backslash notation.
Boolean Literals: true and false
A boolean literal is one of the two values true or false, written without quotation marks. These two words are the only boolean literals in Java.
Because true and false are written without quotes, Java recognizes them as boolean values rather than as text. The quotes are what turn a word into text, so the same letters inside double quotes no longer mean the boolean value. A common first guess is that true and false are simply ordinary words that Java somehow figures out. That guess leads to mistakes, because the spelling and punctuation of the literal are what carry its type.
- ready receives the boolean literal true, with no quotes
- finished receives the boolean literal false, with no quotes
- label receives the String literal "true", which is text because of the double quotes
String Literals and the Empty String
A String literal is text enclosed in double quotation marks. The double quotes delimit the literal. They are not part of the String value that results.
The quotation marks are part of Java's notation, but they do not become characters in the String. A String literal such as "Hello" holds five characters, H, e, l, l, and o, and nothing else. The empty String is written as two double quotes with nothing between them. It contains zero characters, which is different from a String containing a single space.
- greeting holds five characters, the quotes are not included
- empty holds zero characters, written as two adjacent double quotes
spaceOnlyholds one space character between its quotes, so it is not empty
Char Literals and Single Quotes
Single quotes and double quotes indicate different literal categories. A letter in single quotes, such as 'a', is a char literal. The same letter in double quotes, such as "a", is a String literal. The letters look the same, but the quotation marks tell Java which category the literal belongs to. Mixing them up is one of the most common sources of surprising results.
| Feature | Char literal | String literal |
|---|---|---|
| Example | 'a' | "a" |
| Quotation marks | Single quotes | Double quotes |
| Category | Different from String | Text enclosed in double quotes |
| Mixing the marks | Writing "a" instead changes it to text | Writing 'a' instead changes it to a char |
The same letter becomes a different kind of literal depending only on its quotation marks.
Backslash Notation Inside Strings
Some characters cannot be placed directly inside a String literal. A double quote is the clearest case. If a double quote appeared unchanged in the middle of a String literal, Java would read it as the closing quote and the literal would end early. The backslash solves this. It introduces a special representation, so that the character following it is treated as part of the String rather than as notation. The same mechanism covers the other special characters.
| Written in the literal | Character held in the String | Meaning |
|---|---|---|
| backslash followed by a double quote | a double quote character | Quotation mark inside the text |
| backslash followed by n | a linefeed character | Starts a new line |
| backslash followed by t | a tab character | Horizontal tab |
| two backslashes | one backslash character | A literal backslash |
Each backslash notation becomes exactly one character in the resulting String.
The source example combines several of these ideas in one literal. The text is ordinary words, then escaped double quotes surround the question, and a linefeed notation appears at the end. Read left to right, the literal is three parts: ordinary text, escaped quotes around the question, and a trailing linefeed. The backslashes are instructions in the notation, and they allow the special characters to appear in the resulting String.
- The opening and closing double quotes delimit the whole literal
- Each \" is one escaped double quote character in the String
- The final \n is one linefeed character in the String
Tracing a Literal with Several Escapes
Consider a literal with three different escape notations and one doubled backslash. Each notation is read as a unit, and the question is how many characters the String finally holds. Work through it before checking the result.
- a is an ordinary character
- \t becomes one tab character
- b is an ordinary character
- \\ becomes one backslash character
- n is an ordinary letter, because the backslash before it was already consumed by the doubled backslash
What do you think happens?
How many characters does the String s hold after the literal "a\tb\\n" is read?
Reveal answer
5
The characters are a, a tab, b, a single backslash, and n. The \t is one character, and the \\ is one backslash. The final n is a plain letter, not a linefeed, because the doubled backslash already used the backslash that would otherwise have started an escape.
Mistakes Beginners Make with Literals
Common mistake Writing true or false inside double quotes and expecting a boolean value
What it looks likeboolean flag = "true";Why it failsThe double quotes make the value a String literal, so it does not match a boolean variable.
Do this insteadWrite boolean flag = true; without quotes.
Common mistake Using single quotes around a word of several letters
What it looks likeString name = 'Ana';Why it failsSingle quotes indicate a char literal, which is a different literal category from a String literal.
Do this insteadUse double quotes for text: String name = "Ana";Common mistake Writing a double quote inside a String without a backslash
What it looks likeString line = "She said "hi"";Why it failsThe second double quote ends the String literal early, so the rest of the line is read as notation.
Do this insteadEscape each inner quote: String line = "She said \"hi\"";Common mistake Assuming the quotation marks become part of the String value
Why it failsThe double quotes delimit the literal and are not part of the String value.
Do this insteadCount only the characters between the quotes when you reason about a String's contents.
Common mistake Treating "" as if it contained a space
What it looks likeString empty = "";Why it failsThe empty String has zero characters. A space would appear as " " with one character between the quotes.
Do this insteadUse "" for an empty String and " " only when a single space is intended.
Finding the Source of a Surprising Result
When a literal produces an unexpected value, the cause usually falls into one of three groups: the literal's type, its quotation marks, or its special-character notation. The flowchart below shows a sequence of checks that narrows down which one is responsible. Start from the unexpected result and move through the questions in order.
The flowchart reflects the source's advice to inspect a literal from left to right. A boolean problem usually diverges at the spelling or quotation marks. A String problem often diverges at the opening or closing quote, at an accidental single quote, or at a special character written without backslash notation.
Practice Reading Literals
For each literal, decide whether it is a boolean literal, a String literal, or a char literal, and state how many characters a String literal would hold: (1) false (2) "false" (3) 'x' (4) "" (5) "Line one\nLine two"
Hints
- Start by checking the first character of each literal.
- A bare word is a boolean literal only if it is exactly true or false.
- Count the characters between the quotes, and remember that each backslash notation counts as one character.
A teammate wrote String report = "Total: 10\t\\done"; and says the output shows a backslash followed by the letter d instead of a tab. Use the left-to-right method to find where the notation is misread, then rewrite the literal so it contains a tab followed by a single backslash and then the word done.
Hints
- Count what \t and \\ each become in the String.
- A doubled backslash becomes one backslash character, and it is read as one unit.
Key Takeaways
- true and false are the only boolean literals in Java, and they are written without quotation marks.
- Double quotes make a String literal, and the quotation marks delimit the literal without becoming part of the String value. The empty String is written as two adjacent double quotes.
- Single quotes indicate a char literal, which is a different category from a String literal even when the letter is the same.
- The backslash introduces special notation so that quotation marks, linefeeds, tabs, and backslashes can appear inside a String, with each notation becoming one character.
- When a literal behaves unexpectedly, check its type, its quotation marks, and its special-character notation in that order, reading from left to right.