Concepts / Variable Assignment in Java

Variable Assignment in Java

Use single quotes to write a Java character literal.

  • Programming

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?

  • A character literal holding the letter A

  • An identifier, such as a variable or method name

  • A number equal to 65

Reveal answer
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.

'A' in singlequotescharacter literalchar valueone character"A" in doublequotesnot a char literaldifferent typenot a charis read asis read as
How does the type of 'A' differ from the type of "A", and why does the compiler reject one where the other is expected?

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.

opening quotestarts the char literalnewline characterone char valuebackslashstarts the escapencompletes the escapeclosing quoteends the char literalcombines with nforms one value
How do the backslash and the following character combine to form a single char value inside the quotes?
java
Notes on the code
  • '\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.

numeric literalinteger literalno decimal pointoctalleading 0floating-pointliteralhas a decimal pointhexadecimalleading 0xexponential literalhas an exponent partbinaryleading 0bbase-prefixed literalstarts with 0, 0x, or 0bis a kind ofis a kind ofis a kind ofis a kind ofleading 0 meansleading 0x meansleading 0b means
How do the different numeric literal forms relate to one another and to the default types Java assigns to them?

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.

java
Notes on the code
  • 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.

Before
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.

After
long population = 3000000000L;

The L suffix marks the literal as long, so the value can be stored in a long variable.

java
Notes on the code
  • 9.99f: the f suffix marks the literal as float
  • 5000000000L: the L suffix marks the literal as long

Worked Trace of Three Literals

Worked example

Reading the spelling of a literal to find its type

Decide what kind of literal each of these is: 010, 0x1F, and 2.5f.

  1. 1
    Read 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.

  2. 2
    Read 0x1F

    It begins with 0x, which the source identifies as the hexadecimal indicator. This is a hexadecimal literal.

  3. 3
    Read 2.5f

    It contains a decimal point, so it is a floating-point literal. The f suffix marks it as float.

Result

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 like
    char grade = "A";
    Why it fails

    Single quotation marks identify a character literal. Double quotation marks produce a different kind of literal.

    Do this instead
    char grade = 'A';
  • Common mistake

    Writing a character without any quotation marks

    What it looks like
    char grade = A;
    Why it fails

    Without quotation marks, A can be read as an identifier, such as a variable name, rather than a character.

    Do this instead
    char grade = 'A';
  • Common mistake

    Forgetting that a backslash is needed for a single quote inside a char literal

    What it looks like
    char quote = ''';
    Why it fails

    The second quotation mark closes the literal early, so the rest of the text is not a valid single character.

    Do this instead
    char quote = '\'';
  • Common mistake

    Adding a leading zero to a decimal number

    What it looks like
    int count = 010;
    Why it fails

    A leading 0 indicates an octal literal, so 010 does not store the decimal value ten.

    Do this instead
    int 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 like
    float price = 9.99;
    Why it fails

    A literal with a decimal point is a floating-point literal, and without the f suffix it is not treated as float.

    Do this instead
    float price = 9.99f;
  • Common mistake

    Writing a large integer without the L suffix

    What it looks like
    long population = 3000000000;
    Why it fails

    An integer literal too large for int is not automatically treated as long unless the L suffix is written.

    Do this instead
    long population = 3000000000L;

What do you think happens?

What value does the literal 010 store in Java?

  • 10

  • 8

  • 2

Reveal answer
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

MEDIUM

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.