Variables and Primitive Types in Java
Use single quotes to write a Java character literal.
Why Java Needs Literal Rules
A literal is a value written directly in source code. When you type 42 or 'A' into a Java program, you are writing a literal, and the compiler has to decide what kind of value it is before the program can run. A first guess might be that a character is simply written as it appears, so the letter A is written A and the asterisk is written *. Java does not work that way. Without quotation marks, A can be read as an identifier, a name for something such as a variable, and * can be read as the multiplication operator. The same symbols would then mean different things depending on where they appear.
Java solves this ambiguity with syntax. Single quotation marks mark a character literal, and the spelling of a numeric literal carries clues about its type. This article follows the path a literal takes through your reading: first you recognize what the literal is, then you learn how Java interprets its spelling, and finally you check your reading against the mistakes that beginners most often make.
Writing Character Literals
A Java character literal is a single character enclosed in single quotation marks, such as 'A', '*', or '7'. The single quotes tell the compiler that the enclosed symbol is a character value rather than a name or an operator.
The quotation marks matter. The literal 'A' is a single character, and it is a char value. Double quotation marks are a different form used for text strings. A string is not a char, so the two forms belong in different places in your code. When you need one symbol, use single quotes. When you need a sequence of text, use double quotes.
A
*Each of the three declarations stores exactly one character. The symbol '*' is safe to write as a literal only because the quotes wrap it. Without them, the same symbol would be read as the multiplication operator, which is the ambiguity that the single-quote rule removes.
Escape Sequences for Special Characters
Some characters cannot be written plainly inside a literal. A newline or a tab, for example, is an invisible control action rather than a visible symbol, and a single quote inside a character literal would close the literal early. Java handles these cases with a backslash escape. The backslash signals that the character after it does not keep its ordinary meaning. Together the backslash and the following character form one escape sequence that stands for a single special character.
Read each escape as a two-symbol spelling that produces one character. The sequence \n stands for a newline, \t stands for a tab, \' stands for a single quote inside a character literal, and \\ stands for one backslash. The backslash is the only thing that changes the meaning of the character that follows it. Without it, a single quote written inside '' would end the literal, and a lone backslash would start an escape that the compiler cannot finish.
'
\Characters Behind the Scenes
A char holds a numeric code rather than a picture of a letter. In Java, the letter 'A' is stored as the number 65, and the character 'a' is stored as 97. This is why a char can take part in arithmetic. Adding 1 to 'A' produces 66, which is the code for 'B'. The source material for this topic does not go into the full encoding scheme, so treat the numeric codes here as a useful mental model for how char values behave in expressions.
65
BReading Numeric Literal Spellings
Numeric literals are not limited to ordinary decimal notation. A number may appear as a regular integer, a decimal real number, an exponential expression, or a value written in another base. The spelling supplies information about how Java should interpret the literal. Your job as a reader is to notice those clues before you decide what type a value has. The three clues in this article are the leading prefix, the decimal point or exponent, and a trailing suffix.
Before you look at each form, notice that the literal and the type it produces are two separate questions. The literal is what you write. The type is the category Java assigns to it. A spelling with a decimal point or an exponent is read as a floating-point value, while a plain whole number is read as an integer. Suffixes can override the default.
Tracing a Literal to Its Type
Reading a literal is a short procedure. Start with the outer delimiters, then look at the prefix, then at any decimal point or exponent, and finally at the suffix. Each step either settles the type or hands the question to the next step. The table below traces several literals through that procedure.
| Literal | Delimiters or spelling clue | Type Java assigns | Value |
|---|---|---|---|
| 'A' | single quotes around one character | char | the character A, stored as code 65 |
| "A" | double quotes, a text string | String, not char | the text A |
| 42 | plain digits, no prefix | int | 42 |
| 0x2A | prefix 0x | int, hexadecimal | 42 |
| 010 | leading 0 | int, octal | 8 |
| 2.5e3 | exponent | double (floating-point) | 2500.0 |
| 3.14f | f suffix | float | 3.14 |
| 5000000000L | L suffix | long | 5000000000 |
Tracing literal spellings to their types
Working out the type of an unfamiliar literal
Decide the type and value of the literal 0b1111 and of the literal 15.0f.
- 1Check the prefix of 0b1111
The prefix 0b marks a binary literal, so the digits are read in base 2.
- 2Compute the value of 0b1111
Reading 1111 in base 2 gives 8 + 4 + 2 + 1, which is 15.
- 3Check 15.0f
The decimal point signals a floating-point value, and the f suffix fixes the type as float.
- 4Compare the two
Both literals have the value fifteen, but 0b1111 is an int while 15.0f is a float because the spelling and suffix differ.
0b1111 is an int with value 15. 15.0f is a float with value 15.0.
Mistakes Beginners Make
Common mistake Writing a single character with double quotation marks
What it looks likechar grade = "A";Why it failsDouble quotes create a String, and a String is not a char. The declaration does not match its type.
Do this insteadchar grade = 'A';Common mistake Writing a character without quotation marks
What it looks likechar star = *;Why it failsWithout single quotes, the symbol * is read as the multiplication operator rather than as a character.
Do this insteadchar star = '*';Common mistake Assuming a leading zero does not change the value
What it looks likeSystem.out.println(010);Why it failsThe leading 0 marks the literal as octal, so 010 has the value eight, not ten.
Do this insteadWrite 10 for ten, or use the prefix deliberately, such as 0b1010 for binary ten.
Common mistake Assigning a decimal literal to a float without a suffix
What it looks likefloat ratio = 3.14;Why it failsA literal with a decimal point is floating-point by default and is typed as double. Storing it in a float without the f suffix is not allowed.
Do this insteadfloat ratio = 3.14f;Common mistake Forgetting the L suffix on a large integer literal that is meant to be a long
Practice Reading Literals
For each literal below, state whether it is a char, an int, a long, a float, or a double, and give its value: 'x', "x", 0x1F, 0b11, 012, 1.5e2, 7L, 2.0f.
Hints
- Start with the delimiters: single quotes mean char, double quotes mean String.
- A leading 0 followed by x or b changes the base, while a leading 0 alone means octal.
- A decimal point or exponent makes a floating-point literal unless an f or L suffix says otherwise.
Key Takeaways
- A Java character literal is one character written in single quotation marks, and double quotation marks write a text string instead.
- Backslash escapes such as \n, \t, \', and \\ each stand for one special character, even though they are typed as two symbols.
- The spelling of a numeric literal gives clues to its type: a 0, 0x, or 0b prefix sets the base, a decimal point or exponent makes it floating-point, and an L or f suffix fixes it as long or float.
- Common errors involve quotation marks, leading zeroes read as octal, and assigning a double-style literal to a float without the f suffix.