A literal is a value in a program that is provided exactly
as wanted, with no way for it to be changed. It is
sometimes referred to as a constant value.
The following examples show the use of literals in both
Logo and Java.
| What |
Logo |
Java |
| Procedure/Method Argument
|
forward 100 |
forward( 100 ); |
| Addition |
+ :inputParam 12.5 |
someVariable + 12.5 |
| Equality |
equal? :inputParam 0 |
someVariable == 0 |
The first example shows the use of a literal ("100")
as an argument in procedure/method invocations.
The second example shows the use of a literal ("12.5")
in an arithmetic expression.
The third example shows the use of a literal ("0")
in an equality expression.
Java's syntax specifies
rules for how to express constants of a specific types
in source code. Here is a summary:
- boolean
- the text: "true" or "false"
- char
- a single character or an escape sequence surrounded
by apostrophe (single quote) characters
examples: 'A', '\t'
- double
- a real number between -1.7e308 and 1.7e308 with 14
significant figures of accuracy usually expressed in
scientific notation and suffixed with a "d" or "D"
examples: 1.5D, 0.4e-2D
- float
- similar to double but in the range -3.4e38 through
3.4e38 and suffixed with an "f" or "F"
- int
- numbers in the range: -2147483648 through 2147483647
expressed in either octal, decimal, or hexadecimal
formats. The first character(s) making up the number
determine the radix:
- Octal int literals start with a zero and contain
only the octal digits 0 - 7
- Decimal int literals are composed of one or more
digits in the range of 0 - 9
- Hexadecimal literals start with the sequences
"0x" or "0X" (that's zero followed by either an
upper or lower case X) followed by characters
0 - 9 and/or a - f and/or A - F
examples: 0177, 127, and 0x7F (different representations
of the same int value)
- long
- numbers in the range -92233720036854775808 through
92233720036854775807, expressed with the same rules
as type int, but also terminated
with an "l" or "L" character. You should always use
the UPPER CASE variety since the lower case is easily
confused with the number one.
- String
- A String literal is a group of characters surrounded
by double quote characters
examples: "A string", "1234", "Two\nLines"