2018-04-17 15:16:08 -04:00
|
|
|
[[painless-literals]]
|
2017-05-12 19:17:06 -04:00
|
|
|
=== Literals
|
|
|
|
|
2018-04-17 15:16:08 -04:00
|
|
|
Use literals to specify different types of values directly in a script.
|
2017-05-12 19:17:06 -04:00
|
|
|
|
|
|
|
[[integers]]
|
|
|
|
==== Integers
|
|
|
|
|
2018-04-17 15:16:08 -04:00
|
|
|
Use integer literals to specify an integer value in decimal, octal, or hex
|
|
|
|
notation of the <<primitive-types, primitive types>> `int`, `long`, `float`,
|
|
|
|
or `double`. Use the following single letter designations to specify the
|
|
|
|
<<primitive-types, 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.
|
2017-05-12 19:17:06 -04:00
|
|
|
|
2018-04-17 15:16:08 -04:00
|
|
|
*Grammar*
|
2017-05-12 19:17:06 -04:00
|
|
|
[source,ANTLR4]
|
|
|
|
----
|
|
|
|
INTEGER: '-'? ( '0' | [1-9] [0-9]* ) [lLfFdD]?;
|
2018-04-17 15:16:08 -04:00
|
|
|
OCTAL: '-'? '0' [0-7]+ [lL]?;
|
|
|
|
HEX: '-'? '0' [xX] [0-9a-fA-F]+ [lL]?;
|
2017-05-12 19:17:06 -04:00
|
|
|
----
|
|
|
|
|
2018-04-17 15:16:08 -04:00
|
|
|
*Examples*
|
|
|
|
|
2018-04-25 12:38:41 -04:00
|
|
|
* Integer literals.
|
|
|
|
+
|
2018-04-17 15:16:08 -04:00
|
|
|
[source,Painless]
|
2017-05-12 19:17:06 -04:00
|
|
|
----
|
2018-04-25 12:38:41 -04:00
|
|
|
<1> 0
|
|
|
|
<2> 0D
|
|
|
|
<3> 1234L
|
|
|
|
<4> -90f
|
|
|
|
<5> -022
|
|
|
|
<6> 0xF2A
|
2017-05-12 19:17:06 -04:00
|
|
|
----
|
2018-04-25 12:38:41 -04:00
|
|
|
+
|
2018-04-17 15:16:08 -04:00
|
|
|
<1> `int 0`
|
|
|
|
<2> `double 0.0`
|
|
|
|
<3> `long 1234`
|
|
|
|
<4> `float -90.0`
|
|
|
|
<5> `int -18` in octal
|
|
|
|
<6> `int 3882` in hex
|
|
|
|
|
|
|
|
[[floats]]
|
|
|
|
==== Floats
|
2017-05-12 19:17:06 -04:00
|
|
|
|
2018-04-17 15:16:08 -04:00
|
|
|
Use floating point literals to specify a floating point value of the
|
|
|
|
<<primitive-types, primitive types>> `float` or `double`. Use the following
|
|
|
|
single letter designations to specify the <<primitive-types, primitive type>>:
|
|
|
|
`f` or `F` for `float` and `d` or `D` for `double`. If not specified, the type defaults
|
|
|
|
to `double`.
|
2017-05-12 19:17:06 -04:00
|
|
|
|
2018-04-17 15:16:08 -04:00
|
|
|
*Grammar*
|
2017-05-12 19:17:06 -04:00
|
|
|
[source,ANTLR4]
|
|
|
|
----
|
2018-04-17 15:16:08 -04:00
|
|
|
DECIMAL: '-'? ( '0' | [1-9] [0-9]* ) (DOT [0-9]+)? EXPONENT? [fFdD]?;
|
|
|
|
EXPONENT: ( [eE] [+\-]? [0-9]+ );
|
2017-05-12 19:17:06 -04:00
|
|
|
----
|
|
|
|
|
2018-04-17 15:16:08 -04:00
|
|
|
*Examples*
|
|
|
|
|
2018-04-25 12:38:41 -04:00
|
|
|
* Floating point literals.
|
|
|
|
+
|
2018-04-17 15:16:08 -04:00
|
|
|
[source,Painless]
|
2017-05-12 19:17:06 -04:00
|
|
|
----
|
2018-04-25 12:38:41 -04:00
|
|
|
<1> 0.0
|
|
|
|
<2> 1E6
|
|
|
|
<3> 0.977777
|
|
|
|
<4> -126.34
|
|
|
|
<5> 89.9F
|
2017-05-12 19:17:06 -04:00
|
|
|
----
|
2018-04-25 12:38:41 -04:00
|
|
|
+
|
2018-04-17 15:16:08 -04:00
|
|
|
<1> `double 0.0`
|
|
|
|
<2> `double 1000000.0` in exponent notation
|
|
|
|
<3> `double 0.977777`
|
|
|
|
<4> `double -126.34`
|
|
|
|
<5> `float 89.9`
|
|
|
|
|
2017-05-12 19:17:06 -04:00
|
|
|
[[strings]]
|
|
|
|
==== Strings
|
|
|
|
|
2018-04-25 12:38:41 -04:00
|
|
|
Use string literals to specify <<string-type, String>> values 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.
|
2017-05-12 19:17:06 -04:00
|
|
|
|
2018-04-17 15:16:08 -04:00
|
|
|
*Grammar*
|
2017-05-12 19:17:06 -04:00
|
|
|
[source,ANTLR4]
|
|
|
|
----
|
2018-04-17 15:16:08 -04:00
|
|
|
STRING: ( '"' ( '\\"' | '\\\\' | ~[\\"] )*? '"' )
|
|
|
|
| ( '\'' ( '\\\'' | '\\\\' | ~[\\'] )*? '\'' );
|
2017-05-12 19:17:06 -04:00
|
|
|
----
|
|
|
|
|
2018-04-17 15:16:08 -04:00
|
|
|
*Examples*
|
|
|
|
|
2018-04-25 12:38:41 -04:00
|
|
|
* String literals using single-quotes.
|
|
|
|
+
|
2018-04-17 15:16:08 -04:00
|
|
|
[source,Painless]
|
2017-05-12 19:17:06 -04:00
|
|
|
----
|
2018-04-17 15:16:08 -04:00
|
|
|
'single-quoted string literal'
|
2018-04-25 12:38:41 -04:00
|
|
|
'\'single-quoted with escaped single-quotes\' and backslash \\'
|
|
|
|
'single-quoted with non-escaped "double-quotes"'
|
2017-05-12 19:17:06 -04:00
|
|
|
----
|
2018-04-25 12:38:41 -04:00
|
|
|
+
|
|
|
|
* String literals using double-quotes.
|
|
|
|
+
|
2018-04-17 15:16:08 -04:00
|
|
|
[source,Painless]
|
|
|
|
----
|
|
|
|
"double-quoted string literal"
|
2018-04-25 12:38:41 -04:00
|
|
|
"\"double-quoted with escaped double-quotes\" and backslash: \\"
|
|
|
|
"double-quoted with non-escaped 'single-quotes'"
|
2018-04-17 15:16:08 -04:00
|
|
|
----
|
2017-05-12 19:17:06 -04:00
|
|
|
|
2018-04-17 15:16:08 -04:00
|
|
|
[[characters]]
|
|
|
|
==== Characters
|
|
|
|
|
|
|
|
Use the <<painless-casting, casting operator>> to convert string literals or
|
|
|
|
<<string-type, String>> values into <<primitive-types, char>> values.
|
|
|
|
<<string-type, String>> values converted into
|
|
|
|
<<primitive-types, char>> values must be exactly one character in length
|
|
|
|
or an error will occur.
|
|
|
|
|
|
|
|
*Examples*
|
|
|
|
|
2018-04-25 12:38:41 -04:00
|
|
|
* Casting string literals into <<primitive-types, char>> values.
|
|
|
|
+
|
2018-04-17 15:16:08 -04:00
|
|
|
[source,Painless]
|
2017-05-12 19:17:06 -04:00
|
|
|
----
|
|
|
|
(char)"C"
|
|
|
|
(char)'c'
|
2018-04-17 15:16:08 -04:00
|
|
|
----
|
2018-04-25 12:38:41 -04:00
|
|
|
+
|
|
|
|
* Casting a <<string-type, String>> value into a <<primitive-types, char>> value.
|
|
|
|
+
|
2018-04-17 15:16:08 -04:00
|
|
|
[source,Painless]
|
|
|
|
----
|
|
|
|
String s = "s";
|
|
|
|
char c = (char)s;
|
|
|
|
----
|