Literals
editLiterals
editUse literals to specify different types of values directly in a script.
Integers
editUse integer literals to specify an integer value in decimal, octal, or hex
notation of the primitive types int
, long
, float
,
or double
. Use the following single letter designations to specify the
primitive type: l
or L
for long
, f
or F
for
float
, and d
or D
for double
. If not specified, the type defaults to
int
. Use 0
as a prefix to specify an integer literal as octal, and use
0x
or 0X
as a prefix to specify an integer literal as hex.
Grammar
INTEGER: '-'? ( '0' | [1-9] [0-9]* ) [lLfFdD]?; OCTAL: '-'? '0' [0-7]+ [lL]?; HEX: '-'? '0' [xX] [0-9a-fA-F]+ [lL]?;
Examples
Integer literals.
Floats
editUse floating point literals to specify a floating point value of the
primitive types float
or double
. Use the following
single letter designations to specify the primitive type:
f
or F
for float
and d
or D
for double
. If not specified, the type defaults
to double
.
Grammar
DECIMAL: '-'? ( '0' | [1-9] [0-9]* ) (DOT [0-9]+)? EXPONENT? [fFdD]?; EXPONENT: ( [eE] [+\-]? [0-9]+ );
Examples
Floating point literals.
Strings
editUse string literals to specify string values of the
String type with either single-quotes or double-quotes.
Use a \"
token to include a double-quote as part of a double-quoted string
literal. Use a \'
token to include a single-quote as part of a single-quoted
string literal. Use a \\
token to include a backslash as part of any string
literal.
Grammar
STRING: ( '"' ( '\\"' | '\\\\' | ~[\\"] )*? '"' ) | ( '\'' ( '\\\'' | '\\\\' | ~[\\'] )*? '\'' );
Examples
String literals using single-quotes.
'single-quoted string literal' '\'single-quoted string with escaped single-quotes\' and backslash \\' 'single-quoted string with non-escaped "double-quotes"'
String literals using double-quotes.
"double-quoted string literal" "\"double-quoted string with escaped double-quotes\" and backslash: \\" "double-quoted string with non-escaped 'single-quotes'"
Characters
editUse the casting operator to convert string literals or String values into char values. String values converted into char values must be exactly one character in length or an error will occur.
Examples
Casting string literals into char values.
(char)"C" (char)'c'
Casting a String value into a char value.
String s = "s"; char c = (char)s;