Variable Assignment in Java
Use single quotes to write a Java character literal.
Why the Spelling of a Literal Matters
When you assign a value to a variable in Java, the value is written directly in your source code. Such a written value is called a literal. A variable can only receive a value that the compiler can recognize, so the way you spell a value is not a cosmetic choice. Spelling tells Java what kind of value you mean.
A common first assumption is that a character is written exactly as it appears. You might expect the letter A to be written as A, or the asterisk as *. In Java that assumption creates ambiguity. A without quotation marks can be read as an identifier, which is a name such as a variable or method, and * can be read as the multiplication operator. Java therefore uses single quotation marks to say that you mean a character literal.
What do you think happens?
If you write A with no quotation marks in Java, what might the compiler think it is?
Reveal answer
An identifier, such as a variable or method name
Without quotation marks, A can be treated as an identifier. Only the single-quoted form 'A' is a character literal.
Tracing a Quoted Character
Follow what happens when the compiler reads a character literal. It sees an opening single quotation mark, then one character, then a closing single quotation mark. Those three parts together form a single char value. The quotation marks are not stored as part of the value. They are the signal that tells Java to treat the content as a character rather than as a name or an operator.
The comparison shows the key point. Single quotes build a char literal. Double quotes build a different kind of literal. Where Java expects a char, a double-quoted A does not satisfy that expectation, so the compiler rejects it. Keep this in mind whenever you write a single character value.
Escape Sequences Inside Char Literals
Some characters cannot be typed directly inside single quotes in a readable way, and some characters already have a meaning in Java syntax. For these, Java uses a backslash escape. The backslash does not stand alone. It combines with the character that follows it to form one character value. For example, the two-character sequence backslash followed by n represents a single newline character inside a char literal.
The escape sequences used in this article are the ones the source highlights: a backslash n for newline, a backslash followed by a single quotation mark for a single quote inside a char literal, and a double backslash for a literal backslash. Each of these uses two characters in the source code but produces one char value.
- '\n' is one char value: a newline, written with two source characters
- '\'' is one char value: a single quotation mark, needing the backslash so it does not close the literal
- '\\' is one char value: a single backslash
- 'A' is an ordinary character written without an escape
Classifying Numeric Literal Forms
Java numeric literals are not limited to ordinary decimal notation. A number may appear as a regular integer, as a decimal real number, as an exponential expression, or as a value written in another number base. The spelling supplies information about how Java should interpret the literal. Read the literal's shape as a set of clues: a decimal point, an exponent, a suffix, or a prefix each signals something about its type.
The source names the three base indicators directly: a leading 0 indicates octal, 0x indicates hexadecimal, and 0b indicates binary. Decimal points and exponents indicate real-number literals, which Java treats as floating-point values. Integer literals have neither a decimal point nor an exponent.
- 10 is a plain integer literal
- 010 has a leading 0, so it is read as octal
- 0x1F has a 0x prefix, so it is read as hexadecimal
- 0b101 has a 0b prefix, so it is read as binary
- 3.5 has a decimal point, so it is a floating-point literal
- 1.5e3 has an exponent part, so it is an exponential literal
Suffixes That Change a Literal's Type
Sometimes the default reading of a literal is not the type you need. A suffix placed at the end of a numeric literal tells Java which type to use. The L suffix marks a literal as long, and the f suffix marks a literal as float. The suffix is part of the literal's spelling, so it is one of the clues the source describes when it says that suffixes are read as type-related information.
long population = 3000000000;The literal has no suffix, so Java reads it as an ordinary integer literal, and a value this large does not fit the default integer type.
long population = 3000000000L;The L suffix marks the literal as long, so the value can be stored in a long variable.
- 9.99f: the f suffix marks the literal as float
- 5000000000L: the L suffix marks the literal as long
Worked Trace of Three Literals
Reading the spelling of a literal to find its type
Decide what kind of literal each of these is: 010, 0x1F, and 2.5f.
- 1Read 010
It begins with a leading 0 and has no decimal point. The source says a leading 0 indicates octal, so this is an octal literal.
- 2Read 0x1F
It begins with 0x, which the source identifies as the hexadecimal indicator. This is a hexadecimal literal.
- 3Read 2.5f
It contains a decimal point, so it is a floating-point literal. The f suffix marks it as float.
010 is an octal literal, 0x1F is a hexadecimal literal, and 2.5f is a float literal.
Mistakes With Quotes, Leading Zeros, and Floats
Common mistake Writing a single character with double quotation marks when a char is expected
What it looks likechar grade = "A";Why it failsSingle quotation marks identify a character literal. Double quotation marks produce a different kind of literal.
Do this insteadchar grade = 'A';Common mistake Writing a character without any quotation marks
What it looks likechar grade = A;Why it failsWithout quotation marks, A can be read as an identifier, such as a variable name, rather than a character.
Do this insteadchar grade = 'A';Common mistake Forgetting that a backslash is needed for a single quote inside a char literal
What it looks likechar quote = ''';Why it failsThe second quotation mark closes the literal early, so the rest of the text is not a valid single character.
Do this insteadchar quote = '\'';Common mistake Adding a leading zero to a decimal number
What it looks likeint count = 010;Why it failsA leading 0 indicates an octal literal, so 010 does not store the decimal value ten.
Do this insteadint count = 10; (write 010 only when octal is intended)Common mistake Assigning a decimal-point literal to a float without the f suffix
What it looks likefloat price = 9.99;Why it failsA literal with a decimal point is a floating-point literal, and without the f suffix it is not treated as float.
Do this insteadfloat price = 9.99f;Common mistake Writing a large integer without the L suffix
What it looks likelong population = 3000000000;Why it failsAn integer literal too large for int is not automatically treated as long unless the L suffix is written.
Do this insteadlong population = 3000000000L;
What do you think happens?
What value does the literal 010 store in Java?
Reveal answer
8
The leading 0 marks the literal as octal, so 010 in base eight is 8 in decimal. It does not store ten.
Checking Your Literal Choices
For each line below, decide whether the literal is correct for the variable type shown. If it is not, rewrite it. Lines: char a = "B"; long big = 4000000000; float f = 1.25; int bits = 0b1010; int oddDecimal = 0100;
Hints
- A char literal needs single quotation marks around one character.
- A number too large for int needs the L suffix to be treated as long.
- A literal with a decimal point needs the f suffix to be treated as float.
- A leading 0 means octal, and 0b means binary.
Key Takeaways
- A Java char literal is one character written inside single quotation marks, and the quotation marks are what tell Java it is a character rather than an identifier or operator.
- A backslash escape combines with the following character to form one char value, as in newline, single quote, and backslash.
- The spelling of a numeric literal signals its kind: a decimal point or exponent marks a floating-point or exponential value, and a leading 0, 0x, or 0b marks octal, hexadecimal, or binary.
- The L suffix makes a literal long and the f suffix makes a literal float, so choose the suffix that matches the type you need.
- Most literal mistakes come from quotation marks, a stray leading zero, or a missing suffix, so check the spelling against the type before assigning.