Video lecture
Watch on YouTube(opens in a new tab)Sub topic 8
Writing Primitive Literals
A literal is a value you write directly in Java code. For example, 42 is a whole-number literal, and true is a boolean literal.
Literals matter because Java reads their spelling to decide what kind of value they represent. The same-looking value may need different punctuation or a suffix depending on how you want to use it.
You should already be familiar with the basic structure of a Java program.
Literal shapes
| Kind of value | Example | What to notice |
|---|---|---|
| Whole number | `42` | No decimal point |
| Long whole number | `3_000_000_000L` | Ends in `L` |
| Decimal number | `19.99` | Has a decimal point |
| Float value | `20.5f` | Ends in `f` |
| Character | `'A'` | Uses single quotes |
| Boolean | `true` | Has no quotes |
A suffix is a letter added to the end of a literal. It tells Java more about the literal’s type.
A minus sign can appear before a number, as in -12. More precisely, 12 is the literal, and - is an operator that makes the value negative.
Whole-number literals
You can write a whole number with digits:
0421000
A whole-number literal without a suffix normally has the type int. An int is Java’s usual primitive type for whole numbers.
The `L` suffix
Add L to write a long literal. A long is a primitive type that can hold a wider range of whole numbers than an int.
For example, 3_000_000_000L is a long literal. The L matters because the positive value 3_000_000_000 is too large for an int literal.
You can also write 10L. But a long variable can hold a small int literal too. For example, long total = 10; is valid.
Use uppercase L rather than lowercase l. Lowercase l can look like the digit 1.
Separating digits
An underscore can make a long number easier to read. Java ignores underscores inside the digits of a number:
1000000and1_000_000have the same value.3_000_000_000Lis easier to scan than3000000000L.
Put underscores inside groups of digits. For example, 1_000 is valid, but 1000_ is not. Do not put an underscore next to a decimal point or just before a suffix: 1_.5 and 10_L are invalid.
Other ways to write whole numbers
Java also lets you write whole-number literals in other bases. A base is a system for writing numbers using a particular set of digits.
| Form | Example | Value in familiar base ten |
|---|---|---|
| Decimal, or base ten | `42` | 42 |
| Binary, or base two | `0b101010` | 42 |
| Hexadecimal, or base sixteen | `0x2A` | 42 |
| Octal, or base eight | `052` | 42 |
Binary uses only 0 and 1. Hexadecimal uses 0 through 9 and the letters A through F. The letters can be uppercase or lowercase. Octal uses 0 through 7.
The prefix 0b marks a binary literal, and 0x marks a hexadecimal literal. You can use underscores within their digits, as in 0b1010_1010. Do not put an underscore immediately after the prefix: 0b_1010 is invalid.
Decimal and floating-point literals
Java uses floating-point values for numbers that can have a fractional part. A fractional part is the part after the decimal point, such as .5 in 20.5.
A decimal literal such as 19.99 normally has the type double. A double is Java’s usual primitive type for floating-point values.
To write a float literal, add f or F:
20.5fis afloatliteral.20.5is adoubleliteral.20.5dis also adoubleliteral; itsdsuffix is optional.
A float is another floating-point type. The suffix matters when you write a value directly into a float variable: float temperature = 20.5f; is valid, but float temperature = 20.5; is not. Without f, the literal is a double.
Exponent notation
You can use e or E to write a floating-point literal with a power of ten. This is useful for very large or very small values.
1.2e3means 1.2 times 1,000, so its value is 1,200.2.5e-2means 2.5 divided by 100, so its value is 0.025.
These are double literals unless you add f or F. The e does not mean a variable name here; it is part of the number’s spelling.
Some decimal values cannot be stored exactly as floating-point values. The written literal is still useful, but the stored value can be an approximation.
Character literals
A char literal uses single quotes, as in 'A' or '7'. The second example is a character, not the whole number 7.
Some characters are awkward to write directly. An escape sequence is a backslash followed by another character that represents a special value.
| Character literal | Meaning |
|---|---|
| `'A'` | The letter A |
| `'\n'` | A new line |
| `'\t'` | A tab |
| `'\''` | A single quote |
| `'\\'` | A backslash |
Each example represents one char value, even when its written form uses several symbols.
Unicode escapes
Unicode is a standard that assigns numbers to characters. Java can use a Unicode escape to write a character value. For example, '\u0042' represents B. The four digits after \u are hexadecimal digits.
A Java char holds one 16-bit Unicode unit. Some characters, including many emoji, need two such units. They cannot fit in a single char literal.
Do not confuse single and double quotes. 'A' is a char literal. "A" is a string literal: it represents text and is not a primitive literal.
Boolean literals
A boolean is a primitive type with two possible values. Its literals are:
truefalse
Write them in lowercase and without quotes. True is not a boolean literal, and "true" is text, not a boolean value. The number 1 is not a Java boolean literal either.
Worked example
Save this program in a file named LiteralExample.java. It declares variables using several kinds of literals and prints their values.
class LiteralExample {
public static void main(String[] args) {
byte small = 10;
short medium = 1_000;
int count = 42;
long large = 3_000_000_000L;
int hex = 0x2A;
int binary = 0b101010;
int octal = 052;
float temperature = 20.5f;
double price = 19.99;
double distance = 1.2e3;
char grade = 'A';
char unicodeLetter = '\u0042';
char quote = '\'';
boolean ready = true;
System.out.println("Whole: " + small + ", " + medium + ", " + count + ", " + large);
System.out.println("Bases: " + hex + ", " + binary + ", " + octal);
System.out.println("Decimals: " + temperature + ", " + price + ", " + distance);
System.out.println("Characters: " + grade + ", " + unicodeLetter + ", " + quote);
System.out.println("Ready: " + ready);
}
}Compile and run it from the folder containing the file:
javac LiteralExample.java
java LiteralExampleThe output is:
Whole: 10, 1000, 42, 3000000000
Bases: 42, 42, 42
Decimals: 20.5, 19.99, 1200.0
Characters: A, B, '
Ready: trueNotice that the three literals in hex, binary, and octal print the same value. Their written bases differ, but each represents 42.
The double-quoted pieces, such as "Whole: ", are string literals used as labels. The + signs in the print lines join those labels and values into text.
Check your literals
When Java reports an error near a literal, check its spelling:
- For a large whole number, does it need an
Lsuffix? - For a
float, did you addforF? - Are underscores inside digit groups rather than beside a decimal point, prefix, or suffix?
- Did you accidentally start a decimal whole number with
0? - Does a character use single quotes, and does a boolean use lowercase without quotes?
Exercise
Which literals would you use for a float value of 3.5, a char containing the digit 3, and the boolean value false?
Show answer
Use 3.5f, '3', and false. The f makes the first literal a float; single quotes make the second a char; false needs no quotes.
Exercise
What value does 0b1010 represent in decimal? Why is 08 not a valid Java whole-number literal?
Show answer
0b1010 represents 10 in decimal. Java treats 08 as an octal form because it starts with 0, but octal digits can only be 0 through 7.