painless-variables.asciidoc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. [[painless-variables]]
  2. === Variables
  3. A variable loads and stores a value for evaluation during
  4. <<painless-operators, operations>>.
  5. [[variable-declaration]]
  6. ==== Declaration
  7. Declare a variable before use with the format of <<painless-types, type>>
  8. followed by <<painless-identifiers, identifier>>. Declare an
  9. <<array-type, array type>> variable using an opening `[` token and a closing `]`
  10. token for each dimension directly after the identifier. Specify a
  11. comma-separated list of identifiers following the type to declare multiple
  12. variables in a single statement. Use an
  13. <<variable-assignment, assignment operator>> combined with a declaration to
  14. immediately assign a value to a variable. A variable not immediately assigned a
  15. value will have a default value assigned implicitly based on the type.
  16. *Errors*
  17. * If a variable is used prior to or without declaration.
  18. *Grammar*
  19. [source,ANTLR4]
  20. ----
  21. declaration : type ID assignment? (',' ID assignment?)*;
  22. type: ID ('.' ID)* ('[' ']')*;
  23. assignment: '=' expression;
  24. ----
  25. *Examples*
  26. * Different variations of variable declaration.
  27. +
  28. [source,Painless]
  29. ----
  30. <1> int x;
  31. <2> List y;
  32. <3> int x, y = 5, z;
  33. <4> def d;
  34. <5> int i = 10;
  35. <6> float[] f;
  36. <7> Map[][] m;
  37. ----
  38. +
  39. <1> declare `int x`;
  40. store default `null` to `x`
  41. <2> declare `List y`;
  42. store default `null` to `y`
  43. <3> declare `int x`;
  44. store default `int 0` to `x`;
  45. declare `int y`;
  46. store `int 5` to `y`;
  47. declare `int z`;
  48. store default `int 0` to `z`;
  49. <4> declare `def d`;
  50. store default `null` to `d`
  51. <5> declare `int i`;
  52. store `int 10` to `i`
  53. <6> declare `float[] f`;
  54. store default `null` to `f`
  55. <7> declare `Map[][] m`;
  56. store default `null` to `m`
  57. [[variable-assignment]]
  58. ==== Assignment
  59. Use the `assignment operator '='` to store a value in a variable for use in
  60. subsequent operations. Any operation that produces a value can be assigned to
  61. any variable as long as the <<painless-types, types>> are the same or the
  62. resultant type can be <<painless-casting, implicitly cast>> to the variable
  63. type.
  64. *Errors*
  65. * If the type of value is unable to match the type of variable.
  66. *Grammar*
  67. [source,ANTLR4]
  68. ----
  69. assignment: ID '=' expression
  70. ----
  71. *Examples*
  72. * Variable assignment with an integer literal.
  73. +
  74. [source,Painless]
  75. ----
  76. <1> int i;
  77. <2> i = 10;
  78. ----
  79. +
  80. <1> declare `int i`;
  81. store default `int 0` to `i`
  82. <2> store `int 10` to `i`
  83. +
  84. * Declaration combined with immediate assignment.
  85. +
  86. [source,Painless]
  87. ----
  88. <1> int i = 10;
  89. <2> double j = 2.0;
  90. ----
  91. +
  92. <1> declare `int i`;
  93. store `int 10` to `i`
  94. <2> declare `double j`;
  95. store `double 2.0` to `j`
  96. +
  97. * Assignment of one variable to another using primitive type values.
  98. +
  99. [source,Painless]
  100. ----
  101. <1> int i = 10;
  102. <2> int j = i;
  103. ----
  104. +
  105. <1> declare `int i`;
  106. store `int 10` to `i`
  107. <2> declare `int j`;
  108. load from `i` -> `int 10`;
  109. store `int 10` to `j`
  110. +
  111. * Assignment with reference types using the
  112. <<new-instance-operator, new instance operator>>.
  113. +
  114. [source,Painless]
  115. ----
  116. <1> ArrayList l = new ArrayList();
  117. <2> Map m = new HashMap();
  118. ----
  119. +
  120. <1> declare `ArrayList l`;
  121. allocate `ArrayList` instance -> `ArrayList reference`;
  122. store `ArrayList reference` to `l`
  123. <2> declare `Map m`;
  124. allocate `HashMap` instance -> `HashMap reference`;
  125. implicit cast `HashMap reference` to `Map reference` -> `Map reference`;
  126. store `Map reference` to `m`
  127. +
  128. * Assignment of one variable to another using reference type values.
  129. +
  130. [source,Painless]
  131. ----
  132. <1> List l = new ArrayList();
  133. <2> List k = l;
  134. <3> List m;
  135. <4> m = k;
  136. ----
  137. +
  138. <1> declare `List l`;
  139. allocate `ArrayList` instance -> `ArrayList reference`;
  140. implicit cast `ArrayList reference` to `List reference` -> `List reference`;
  141. store `List reference` to `l`
  142. <2> declare `List k`;
  143. load from `l` -> `List reference`;
  144. store `List reference` to `k`;
  145. (note `l` and `k` refer to the same instance known as a shallow-copy)
  146. <3> declare `List m`;
  147. store default `null` to `m`
  148. <4> load from `k` -> `List reference`;
  149. store `List reference` to `m`;
  150. (note `l`, `k`, and `m` refer to the same instance)
  151. +
  152. * Assignment with array type variables using the
  153. <<new-array-operator, new array operator>>.
  154. +
  155. [source,Painless]
  156. ----
  157. <1> int[] ia1;
  158. <2> ia1 = new int[2];
  159. <3> ia1[0] = 1;
  160. <4> int[] ib1 = ia1;
  161. <5> int[][] ic2 = new int[2][5];
  162. <6> ic2[1][3] = 2;
  163. <7> ic2[0] = ia1;
  164. ----
  165. +
  166. <1> declare `int[] ia1`;
  167. store default `null` to `ia1`
  168. <2> allocate `1-d int array` instance with `length [2]`
  169. -> `1-d int array reference`;
  170. store `1-d int array reference` to `ia1`
  171. <3> load from `ia1` -> `1-d int array reference`;
  172. store `int 1` to `index [0]` of `1-d int array reference`
  173. <4> declare `int[] ib1`;
  174. load from `ia1` -> `1-d int array reference`;
  175. store `1-d int array reference` to `ib1`;
  176. (note `ia1` and `ib1` refer to the same instance known as a shallow copy)
  177. <5> declare `int[][] ic2`;
  178. allocate `2-d int array` instance with `length [2, 5]`
  179. -> `2-d int array reference`;
  180. store `2-d int array reference` to `ic2`
  181. <6> load from `ic2` -> `2-d int array reference`;
  182. store `int 2` to `index [1, 3]` of `2-d int array reference`
  183. <7> load from `ia1` -> `1-d int array reference`;
  184. load from `ic2` -> `2-d int array reference`;
  185. store `1-d int array reference` to
  186. `index [0]` of `2-d int array reference`;
  187. (note `ia1`, `ib1`, and `index [0]` of `ia2` refer to the same instance)