Integer
literals
A literal notation for an Integer
value.
Usage
An Integer
literal may be written in a variety of ways.
Integer one = 1;
variable Integer oneMillion = 1000000;
oneMillion = 1_000_000;
oneMillion = 1M;
Description
At its simplest, an Integer
literal is just a series of decimal digits,
0
, 1
, 2
, 3
, 4
, 5
, 6
, 7
, 8
and 9
. Other digit characters
(digits from other scripts) are not allowed.
When a negative number is required, the unary minus operator may be used, like this:
Integer minusTwo = -2;
Leading zeros
Integer
literals with a leading zero, 0
, are allowed, but unlike other
C-like programming languages, such literals are not interpreted using
octal notation.
Grouping digits
To make long integer literals easier to read, groups of three digits may be
separated with an underscore, _
, similar to how a comma or stop is used
as a thousands separator in many written numbers. Only the left-most group
may have one or two digits.
Decimal suffixes
Use of one of the following metric magnitudes as a suffix is supported:
-
k
(kilo), 103 -
M
(mega), 106 -
G
(giga), 109 -
T
(tera), 1012 -
P
(peta), 1015
For example:
Integer oneThousand = 1k;
Binary literals
A binary integer can be written with a $
prefix, and again _
may be
used to group digits, but binary digit groups are of length 4.
For example:
Integer eight = $1001; // binary literal
Hexadecimal literals
A hexadecimal integer can be written with a #
prefix, and again _
may be used to group digits, but hexadecimal digit groups are of length
2 or 4.
Integer red = #ff_00_00; // hex literal
As a primary
Invoking members of the class Integer
directly on a literal is permitted:
Integer minusFive = 5.negativeValue;
See also
- Numeric literals in the Tour of Ceylon
- unary plus and unary minus