Opening the reading…
Opening the reading…
PROGRAMMING FUNDAMENTALS › JAVA FUNDAMENTALS
A literal is a piece of Java source code that directly spells a value. In this method call, the arguments are literal spellings, while inspect is a name that identifies a program element. The punctuation inside each argument is part of Java's syntax, not decoration that you can remove freely.
inspect(1_024, 0b100_0000_0000, 02000, 0x400L, 3.5e1F, 35.0, '\n', "Room 7\nOpen", true, false, null);The arguments contain integer literals, floating-point literals, a character literal, a string literal, boolean literals, and the null literal. The four integer spellings near the beginning use different numeric notation, but each denotes the same integer value, 1024. The two floating-point spellings both denote 35. The character and string spellings contain text-related values, while true, false, and null are special literal forms recognized by Java.
Which parts of this statement are literals?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
Single quotes delimit a character literal, which represents one character. Double quotes delimit a string literal, which can represent a sequence of characters. In the running statement, '\n' is one character literal, while "Room 7\nOpen" is a string literal containing several ordinary characters and a newline character.
inspect(1_024, 0b100_0000_0000, 02000, 0x400L, 3.5e1F, 35.0, '\n', "Room 7\nOpen", true, false, null);The backslash starts an escape sequence. The two source characters backslash and n in \n represent one newline character, rather than a backslash followed by the letter n. The quotes are delimiters and are not part of the represented character or string. An actual unescaped line break cannot simply be placed inside either literal, so the escape sequence is the valid way to represent a newline here.
The first four numeric arguments all represent 1024, but their spellings use different bases. 1_024 is decimal, 0b100_0000_0000 is binary, 02000 is octal, and 0x400L is hexadecimal with an L suffix. The prefix tells Java how to read the digits, so removing or changing a prefix can change the value rather than merely changing its formatting.
| LITERAL | NOTATION | VALUE |
|---|---|---|
| 1_024 | decimal | 1024 |
| 0b100_0000_0000 | binary | 1024 |
| 02000 | octal | 1024 |
| 0x400L | hexadecimal | 1024 |
Underscores make long digit sequences easier to read, but they must appear between digits. They cannot begin or end the digits, sit directly beside a base prefix, or be placed where no digit separates them. Thus the underscores in 1_024 and 0b100_0000_0000 are meaningful separators, not comments or decoration.
The floating-point literal 3.5e1F uses exponent notation: 3.5 multiplied by 10 to the power of 1 is 35. The F suffix is part of the literal spelling. The literal 35.0 writes the same numeric value without exponent notation, while 0x400L uses L to mark its integer literal. These suffixes affect how Java recognizes the literal, so they are not optional decoration.
Repair only the malformed literal in this statement. Type the corrected argument, including its quotes and escape.
inspect(1_024, 0b100_0000_0000, 02000, 0x400L, 3.5e1F, 35.0, '\n', 'Room 7\nOpen', true, false, null);Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
In the running statement, true and false are the exact lowercase boolean literals. Java recognizes those unquoted spellings as boolean values. Changing their case or placing them inside quotes changes the source from a boolean literal into a different form, rather than creating another spelling of the same value.
null is a distinct literal. It is not another spelling of false, zero, or an empty string. It must remain lowercase and unquoted to be recognized as null. Quoting it produces string text containing the four letters n, u, l, and l, just as quoting true produces string text rather than the boolean literal true.
inspect(1_024, 0b100_0000_0000, 02000, 0x400L, 3.5e1F, 35.0, '\n', "Room 7\nOpen", true, false, null);Across this one statement, punctuation carries meaning: quotes choose character or string syntax, the backslash introduces an escape, prefixes choose numeric notation, underscores separate digits, suffixes complete numeric spellings, and lowercase unquoted words identify special literal forms. Removing or changing any of those marks can change the value or make the source invalid.