painless-operators-boolean.asciidoc 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420
  1. [[painless-operators-boolean]]
  2. === Operators: Boolean
  3. [[boolean-not-operator]]
  4. ==== Boolean Not
  5. Use the `boolean not operator '!'` to NOT a `boolean` type value where `true` is
  6. flipped to `false` and `false` is flipped to `true`.
  7. *Errors*
  8. * If a value other than a `boolean` type value or a value that is castable to a
  9. `boolean` type value is given.
  10. *Truth*
  11. [options="header",cols="<1,<1"]
  12. |====
  13. | original | result
  14. | true | false
  15. | false | true
  16. |====
  17. *Grammar*
  18. [source,ANTLR4]
  19. ----
  20. boolean_not: '!' expression;
  21. ----
  22. *Examples*
  23. * Boolean not with the `boolean` type.
  24. +
  25. [source,Painless]
  26. ----
  27. <1> boolean x = !false;
  28. <2> boolean y = !x;
  29. ----
  30. <1> declare `boolean x`;
  31. boolean not `boolean false` -> `boolean true`;
  32. store `boolean true` to `x`
  33. <2> declare `boolean y`;
  34. load from `x` -> `boolean true`;
  35. boolean not `boolean true` -> `boolean false`;
  36. store `boolean false` to `y`
  37. +
  38. * Boolean not with the `def` type.
  39. +
  40. [source,Painless]
  41. ----
  42. <1> def y = true;
  43. <2> def z = !y;
  44. ----
  45. +
  46. <1> declare `def y`;
  47. implicit cast `boolean true` to `def` -> `def`;
  48. store `true` to `y`
  49. <2> declare `def z`;
  50. load from `y` -> `def`;
  51. implicit cast `def` to `boolean true` -> boolean `true`;
  52. boolean not `boolean true` -> `boolean false`;
  53. implicit cast `boolean false` to `def` -> `def`;
  54. store `def` to `z`
  55. [[greater-than-operator]]
  56. ==== Greater Than
  57. Use the `greater than operator '>'` to COMPARE two numeric type values where a
  58. resultant `boolean` type value is `true` if the left-hand side value is greater
  59. than to the right-hand side value and `false` otherwise.
  60. *Errors*
  61. * If either the evaluated left-hand side or the evaluated right-hand side is a
  62. non-numeric value.
  63. *Grammar*
  64. [source,ANTLR4]
  65. ----
  66. greater_than: expression '>' expression;
  67. ----
  68. *Promotion*
  69. [cols="<1,^1,^1,^1,^1,^1,^1,^1,^1"]
  70. |====
  71. | | byte | short | char | int | long | float | double | def
  72. | byte | int | int | int | int | long | float | double | def
  73. | short | int | int | int | int | long | float | double | def
  74. | char | int | int | int | int | long | float | double | def
  75. | int | int | int | int | int | long | float | double | def
  76. | long | long | long | long | long | long | float | double | def
  77. | float | float | float | float | float | float | float | double | def
  78. | double | double | double | double | double | double | double | double | def
  79. | def | def | def | def | def | def | def | def | def
  80. |====
  81. *Examples*
  82. * Greater than with different numeric types.
  83. +
  84. [source,Painless]
  85. ----
  86. <1> boolean x = 5 > 4;
  87. <2> double y = 6.0;
  88. <3> x = 6 > y;
  89. ----
  90. +
  91. <1> declare `boolean x`;
  92. greater than `int 5` and `int 4` -> `boolean true`;
  93. store `boolean true` to `x`;
  94. <2> declare `double y`;
  95. store `double 6.0` to `y`;
  96. <3> load from `y` -> `double 6.0 @0`;
  97. promote `int 6` and `double 6.0`: result `double`;
  98. implicit cast `int 6` to `double 6.0 @1` -> `double 6.0 @1`;
  99. greater than `double 6.0 @1` and `double 6.0 @0` -> `boolean false`;
  100. store `boolean false` to `x`
  101. +
  102. * Greater than with `def` type.
  103. +
  104. [source,Painless]
  105. ----
  106. <1> int x = 5;
  107. <2> def y = 7.0;
  108. <3> def z = y > 6.5;
  109. <4> def a = x > y;
  110. ----
  111. +
  112. <1> declare `int x`;
  113. store `int 5` to `x`
  114. <2> declare `def y`;
  115. implicit cast `double 7.0` to `def` -> `def`;
  116. store `def` to `y`
  117. <3> declare `def z`;
  118. load from `y` -> `def`;
  119. implicit cast `def` to `double 7.0` -> `double 7.0`;
  120. greater than `double 7.0` and `double 6.5` -> `boolean true`;
  121. implicit cast `boolean true` to `def` -> `def`;
  122. store `def` to `z`
  123. <4> declare `def a`;
  124. load from `y` -> `def`;
  125. implicit cast `def` to `double 7.0` -> `double 7.0`;
  126. load from `x` -> `int 5`;
  127. promote `int 5` and `double 7.0`: result `double`;
  128. implicit cast `int 5` to `double 5.0` -> `double 5.0`;
  129. greater than `double 5.0` and `double 7.0` -> `boolean false`;
  130. implicit cast `boolean false` to `def` -> `def`;
  131. store `def` to `z`
  132. [[greater-than-or-equal-operator]]
  133. ==== Greater Than Or Equal
  134. Use the `greater than or equal operator '>='` to COMPARE two numeric type values
  135. where a resultant `boolean` type value is `true` if the left-hand side value is
  136. greater than or equal to the right-hand side value and `false` otherwise.
  137. *Errors*
  138. * If either the evaluated left-hand side or the evaluated right-hand side is a
  139. non-numeric value.
  140. *Grammar*
  141. [source,ANTLR4]
  142. ----
  143. greater_than_or_equal: expression '>=' expression;
  144. ----
  145. *Promotion*
  146. [cols="<1,^1,^1,^1,^1,^1,^1,^1,^1"]
  147. |====
  148. | | byte | short | char | int | long | float | double | def
  149. | byte | int | int | int | int | long | float | double | def
  150. | short | int | int | int | int | long | float | double | def
  151. | char | int | int | int | int | long | float | double | def
  152. | int | int | int | int | int | long | float | double | def
  153. | long | long | long | long | long | long | float | double | def
  154. | float | float | float | float | float | float | float | double | def
  155. | double | double | double | double | double | double | double | double | def
  156. | def | def | def | def | def | def | def | def | def
  157. |====
  158. *Examples*
  159. * Greater than or equal with different numeric types.
  160. +
  161. [source,Painless]
  162. ----
  163. <1> boolean x = 5 >= 4;
  164. <2> double y = 6.0;
  165. <3> x = 6 >= y;
  166. ----
  167. +
  168. <1> declare `boolean x`;
  169. greater than or equal `int 5` and `int 4` -> `boolean true`;
  170. store `boolean true` to `x`
  171. <2> declare `double y`;
  172. store `double 6.0` to `y`
  173. <3> load from `y` -> `double 6.0 @0`;
  174. promote `int 6` and `double 6.0`: result `double`;
  175. implicit cast `int 6` to `double 6.0 @1` -> `double 6.0 @1`;
  176. greater than or equal `double 6.0 @1` and `double 6.0 @0` -> `boolean true`;
  177. store `boolean true` to `x`
  178. +
  179. * Greater than or equal with the `def` type.
  180. +
  181. [source,Painless]
  182. ----
  183. <1> int x = 5;
  184. <2> def y = 7.0;
  185. <3> def z = y >= 7.0;
  186. <4> def a = x >= y;
  187. ----
  188. +
  189. <1> declare `int x`;
  190. store `int 5` to `x`;
  191. <2> declare `def y`
  192. implicit cast `double 7.0` to `def` -> `def`;
  193. store `def` to `y`
  194. <3> declare `def z`;
  195. load from `y` -> `def`;
  196. implicit cast `def` to `double 7.0 @0` -> `double 7.0 @0`;
  197. greater than or equal `double 7.0 @0` and `double 7.0 @1` -> `boolean true`;
  198. implicit cast `boolean true` to `def` -> `def`;
  199. store `def` to `z`
  200. <4> declare `def a`;
  201. load from `y` -> `def`;
  202. implicit cast `def` to `double 7.0` -> `double 7.0`;
  203. load from `x` -> `int 5`;
  204. promote `int 5` and `double 7.0`: result `double`;
  205. implicit cast `int 5` to `double 5.0` -> `double 5.0`;
  206. greater than or equal `double 5.0` and `double 7.0` -> `boolean false`;
  207. implicit cast `boolean false` to `def` -> `def`;
  208. store `def` to `z`
  209. [[less-than-operator]]
  210. ==== Less Than
  211. Use the `less than operator '<'` to COMPARE two numeric type values where a
  212. resultant `boolean` type value is `true` if the left-hand side value is less
  213. than to the right-hand side value and `false` otherwise.
  214. *Errors*
  215. * If either the evaluated left-hand side or the evaluated right-hand side is a
  216. non-numeric value.
  217. *Grammar*
  218. [source,ANTLR4]
  219. ----
  220. less_than: expression '<' expression;
  221. ----
  222. *Promotion*
  223. [cols="<1,^1,^1,^1,^1,^1,^1,^1,^1"]
  224. |====
  225. | | byte | short | char | int | long | float | double | def
  226. | byte | int | int | int | int | long | float | double | def
  227. | short | int | int | int | int | long | float | double | def
  228. | char | int | int | int | int | long | float | double | def
  229. | int | int | int | int | int | long | float | double | def
  230. | long | long | long | long | long | long | float | double | def
  231. | float | float | float | float | float | float | float | double | def
  232. | double | double | double | double | double | double | double | double | def
  233. | def | def | def | def | def | def | def | def | def
  234. |====
  235. *Examples*
  236. * Less than with different numeric types.
  237. +
  238. [source,Painless]
  239. ----
  240. <1> boolean x = 5 < 4;
  241. <2> double y = 6.0;
  242. <3> x = 6 < y;
  243. ----
  244. +
  245. <1> declare `boolean x`;
  246. less than `int 5` and `int 4` -> `boolean false`;
  247. store `boolean false` to `x`
  248. <2> declare `double y`;
  249. store `double 6.0` to `y`
  250. <3> load from `y` -> `double 6.0 @0`;
  251. promote `int 6` and `double 6.0`: result `double`;
  252. implicit cast `int 6` to `double 6.0 @1` -> `double 6.0 @1`;
  253. less than `double 6.0 @1` and `double 6.0 @0` -> `boolean false`;
  254. store `boolean false` to `x`
  255. +
  256. * Less than with the `def` type.
  257. +
  258. [source,Painless]
  259. ----
  260. <1> int x = 5;
  261. <2> def y = 7.0;
  262. <3> def z = y < 6.5;
  263. <4> def a = x < y;
  264. ----
  265. +
  266. <1> declare `int x`;
  267. store `int 5` to `x`
  268. <2> declare `def y`;
  269. implicit cast `double 7.0` to `def` -> `def`;
  270. store `def` to `y`
  271. <3> declare `def z`;
  272. load from `y` -> `def`;
  273. implicit cast `def` to `double 7.0` -> `double 7.0`;
  274. less than `double 7.0` and `double 6.5` -> `boolean false`;
  275. implicit cast `boolean false` to `def` -> `def`;
  276. store `def` to `z`
  277. <4> declare `def a`;
  278. load from `y` -> `def`;
  279. implicit cast `def` to `double 7.0` -> `double 7.0`;
  280. load from `x` -> `int 5`;
  281. promote `int 5` and `double 7.0`: result `double`;
  282. implicit cast `int 5` to `double 5.0` -> `double 5.0`;
  283. less than `double 5.0` and `double 7.0` -> `boolean true`;
  284. implicit cast `boolean true` to `def` -> `def`;
  285. store `def` to `z`
  286. [[less-than-or-equal-operator]]
  287. ==== Less Than Or Equal
  288. Use the `less than or equal operator '<='` to COMPARE two numeric type values
  289. where a resultant `boolean` type value is `true` if the left-hand side value is
  290. less than or equal to the right-hand side value and `false` otherwise.
  291. *Errors*
  292. * If either the evaluated left-hand side or the evaluated right-hand side is a
  293. non-numeric value.
  294. *Grammar*
  295. [source,ANTLR4]
  296. ----
  297. greater_than_or_equal: expression '<=' expression;
  298. ----
  299. *Promotion*
  300. [cols="<1,^1,^1,^1,^1,^1,^1,^1,^1"]
  301. |====
  302. | | byte | short | char | int | long | float | double | def
  303. | byte | int | int | int | int | long | float | double | def
  304. | short | int | int | int | int | long | float | double | def
  305. | char | int | int | int | int | long | float | double | def
  306. | int | int | int | int | int | long | float | double | def
  307. | long | long | long | long | long | long | float | double | def
  308. | float | float | float | float | float | float | float | double | def
  309. | double | double | double | double | double | double | double | double | def
  310. | def | def | def | def | def | def | def | def | def
  311. |====
  312. *Examples*
  313. * Less than or equal with different numeric types.
  314. +
  315. [source,Painless]
  316. ----
  317. <1> boolean x = 5 <= 4;
  318. <2> double y = 6.0;
  319. <3> x = 6 <= y;
  320. ----
  321. +
  322. <1> declare `boolean x`;
  323. less than or equal `int 5` and `int 4` -> `boolean false`;
  324. store `boolean true` to `x`
  325. <2> declare `double y`;
  326. store `double 6.0` to `y`
  327. <3> load from `y` -> `double 6.0 @0`;
  328. promote `int 6` and `double 6.0`: result `double`;
  329. implicit cast `int 6` to `double 6.0 @1` -> `double 6.0 @1`;
  330. less than or equal `double 6.0 @1` and `double 6.0 @0` -> `boolean true`;
  331. store `boolean true` to `x`
  332. +
  333. * Less than or equal with the `def` type.
  334. +
  335. [source,Painless]
  336. ----
  337. <1> int x = 5;
  338. <2> def y = 7.0;
  339. <3> def z = y <= 7.0;
  340. <4> def a = x <= y;
  341. ----
  342. +
  343. <1> declare `int x`;
  344. store `int 5` to `x`;
  345. <2> declare `def y`;
  346. implicit cast `double 7.0` to `def` -> `def`;
  347. store `def` to `y`;
  348. <3> declare `def z`;
  349. load from `y` -> `def`;
  350. implicit cast `def` to `double 7.0 @0` -> `double 7.0 @0`;
  351. less than or equal `double 7.0 @0` and `double 7.0 @1` -> `boolean true`;
  352. implicit cast `boolean true` to `def` -> `def`;
  353. store `def` to `z`
  354. <4> declare `def a`;
  355. load from `y` -> `def`;
  356. implicit cast `def` to `double 7.0` -> `double 7.0`;
  357. load from `x` -> `int 5`;
  358. promote `int 5` and `double 7.0`: result `double`;
  359. implicit cast `int 5` to `double 5.0` -> `double 5.0`;
  360. less than or equal `double 5.0` and `double 7.0` -> `boolean true`;
  361. implicit cast `boolean true` to `def` -> `def`;
  362. store `def` to `z`
  363. [[instanceof-operator]]
  364. ==== Instanceof
  365. Use the `instanceof operator` to COMPARE the variable/field type to a
  366. specified reference type using the reference type name where a resultant
  367. `boolean` type value is `true` if the variable/field type is the same as or a
  368. descendant of the specified reference type and false otherwise.
  369. *Errors*
  370. * If the reference type name doesn't exist as specified by the right-hand side.
  371. *Grammar*
  372. [source,ANTLR4]
  373. ----
  374. instance_of: ID 'instanceof' TYPE;
  375. ----
  376. *Examples*
  377. * Instance of with different reference types.
  378. +
  379. [source,Painless]
  380. ----
  381. <1> Map m = new HashMap();
  382. <2> boolean a = m instanceof HashMap;
  383. <3> boolean b = m instanceof Map;
  384. ----
  385. +
  386. <1> declare `Map m`;
  387. allocate `HashMap` instance -> `HashMap reference`;
  388. implicit cast `HashMap reference` to `Map reference`;
  389. store `Map reference` to `m`
  390. <2> declare `boolean a`;
  391. load from `m` -> `Map reference`;
  392. implicit cast `Map reference` to `HashMap reference` -> `HashMap reference`;
  393. instanceof `HashMap reference` and `HashMap` -> `boolean true`;
  394. store `boolean true` to `a`
  395. <3> declare `boolean b`;
  396. load from `m` -> `Map reference`;
  397. implicit cast `Map reference` to `HashMap reference` -> `HashMap reference`;
  398. instanceof `HashMap reference` and `Map` -> `boolean true`;
  399. store `true` to `b`;
  400. (note `HashMap` is a descendant of `Map`)
  401. +
  402. * Instance of with the `def` type.
  403. +
  404. [source,Painless]
  405. ----
  406. <1> def d = new ArrayList();
  407. <2> boolean a = d instanceof List;
  408. <3> boolean b = d instanceof Map;
  409. ----
  410. +
  411. <1> declare `def d`;
  412. allocate `ArrayList` instance -> `ArrayList reference`;
  413. implicit cast `ArrayList reference` to `def` -> `def`;
  414. store `def` to `d`
  415. <2> declare `boolean a`;
  416. load from `d` -> `def`;
  417. implicit cast `def` to `ArrayList reference` -> `ArrayList reference`;
  418. instanceof `ArrayList reference` and `List` -> `boolean true`;
  419. store `boolean true` to `a`;
  420. (note `ArrayList` is a descendant of `List`)
  421. <3> declare `boolean b`;
  422. load from `d` -> `def`;
  423. implicit cast `def` to `ArrayList reference` -> `ArrayList reference`;
  424. instanceof `ArrayList reference` and `Map` -> `boolean false`;
  425. store `boolean false` to `a`;
  426. (note `ArrayList` is not a descendant of `Map`)
  427. [[equality-equals-operator]]
  428. ==== Equality Equals
  429. Use the `equality equals operator '=='` to COMPARE two values where a resultant
  430. `boolean` type value is `true` if the two values are equal and `false`
  431. otherwise. The member method, `equals`, is implicitly called when the values are
  432. reference type values where the first value is the target of the call and the
  433. second value is the argument. This operation is null-safe where if both values
  434. are `null` the resultant `boolean` type value is `true`, and if only one value
  435. is `null` the resultant `boolean` type value is `false`. A valid comparison is
  436. between `boolean` type values, numeric type values, or reference type values.
  437. *Errors*
  438. * If a comparison is made between a `boolean` type value and numeric type value.
  439. * If a comparison is made between a primitive type value and a reference type
  440. value.
  441. *Grammar*
  442. [source,ANTLR4]
  443. ----
  444. equality_equals: expression '==' expression;
  445. ----
  446. *Promotion*
  447. [cols="<1,^1,^1,^1,^1,^1,^1,^1,^1,^1,^1"]
  448. |====
  449. | | boolean | byte | short | char | int | long | float | double | Reference | def
  450. | boolean | boolean | - | - | - | - | - | - | - | - | def
  451. | byte | - | int | int | int | int | long | float | double | - | def
  452. | short | - | int | int | int | int | long | float | double | - | def
  453. | char | - | int | int | int | int | long | float | double | - | def
  454. | int | - | int | int | int | int | long | float | double | - | def
  455. | long | - | long | long | long | long | long | float | double | - | def
  456. | float | - | float | float | float | float | float | float | double | - | def
  457. | double | - | double | double | double | double | double | double | double | - | def
  458. | Reference | - | - | - | - | - | - | - | - | Object | def
  459. | def | def | def | def | def | def | def | def | def | def | def
  460. |====
  461. *Examples*
  462. * Equality equals with the `boolean` type.
  463. +
  464. [source,Painless]
  465. ----
  466. <1> boolean a = true;
  467. <2> boolean b = false;
  468. <3> a = a == false;
  469. <4> b = a == b;
  470. ----
  471. +
  472. <1> declare `boolean a`;
  473. store `boolean true` to `a`
  474. <2> declare `boolean b`;
  475. store `boolean false` to `b`
  476. <3> load from `a` -> `boolean true`;
  477. equality equals `boolean true` and `boolean false` -> `boolean false`;
  478. store `boolean false` to `a`
  479. <4> load from `a` -> `boolean false @0`;
  480. load from `b` -> `boolean false @1`;
  481. equality equals `boolean false @0` and `boolean false @1`
  482. -> `boolean false`;
  483. store `boolean false` to `b`
  484. +
  485. * Equality equals with primitive types.
  486. +
  487. [source,Painless]
  488. ----
  489. <1> int a = 1;
  490. <2> double b = 2.0;
  491. <3> boolean c = a == b;
  492. <4> c = 1 == a;
  493. ----
  494. +
  495. <1> declare `int a`;
  496. store `int 1` to `a`
  497. <2> declare `double b`;
  498. store `double 1.0` to `b`
  499. <3> declare `boolean c`;
  500. load from `a` -> `int 1`;
  501. load from `b` -> `double 2.0`;
  502. promote `int 1` and `double 2.0`: result `double`;
  503. implicit cast `int 1` to `double 1.0` -> `double `1.0`;
  504. equality equals `double 1.0` and `double 2.0` -> `boolean false`;
  505. store `boolean false` to `c`
  506. <4> load from `a` -> `int 1 @1`;
  507. equality equals `int 1 @0` and `int 1 @1` -> `boolean true`;
  508. store `boolean true` to `c`
  509. +
  510. * Equal equals with reference types.
  511. +
  512. [source,Painless]
  513. ----
  514. <1> List a = new ArrayList();
  515. <2> List b = new ArrayList();
  516. <3> a.add(1);
  517. <4> boolean c = a == b;
  518. <5> b.add(1);
  519. <6> c = a == b;
  520. ----
  521. +
  522. <1> declare `List a`;
  523. allocate `ArrayList` instance -> `ArrayList reference`;
  524. implicit cast `ArrayList reference` to `List reference` -> `List reference`;
  525. store `List reference` to `a`
  526. <2> declare `List b`;
  527. allocate `ArrayList` instance -> `ArrayList reference`;
  528. implicit cast `ArrayList reference` to `List reference` -> `List reference`;
  529. store `List reference` to `b`
  530. <3> load from `a` -> `List reference`;
  531. call `add` on `List reference` with arguments (`int 1)`
  532. <4> declare `boolean c`;
  533. load from `a` -> `List reference @0`;
  534. load from `b` -> `List reference @1`;
  535. call `equals` on `List reference @0` with arguments (`List reference @1`)
  536. -> `boolean false`;
  537. store `boolean false` to `c`
  538. <5> load from `b` -> `List reference`;
  539. call `add` on `List reference` with arguments (`int 1`)
  540. <6> load from `a` -> `List reference @0`;
  541. load from `b` -> `List reference @1`;
  542. call `equals` on `List reference @0` with arguments (`List reference @1`)
  543. -> `boolean true`;
  544. store `boolean true` to `c`
  545. +
  546. * Equality equals with `null`.
  547. +
  548. [source,Painless]
  549. ----
  550. <1> Object a = null;
  551. <2> Object b = null;
  552. <3> boolean c = a == null;
  553. <4> c = a == b;
  554. <5> b = new Object();
  555. <6> c = a == b;
  556. ----
  557. +
  558. <1> declare `Object a`;
  559. store `null` to `a`
  560. <2> declare `Object b`;
  561. store `null` to `b`
  562. <3> declare `boolean c`;
  563. load from `a` -> `null @0`;
  564. equality equals `null @0` and `null @1` -> `boolean true`;
  565. store `boolean true` to `c`
  566. <4> load from `a` -> `null @0`;
  567. load from `b` -> `null @1`;
  568. equality equals `null @0` and `null @1` -> `boolean true`;
  569. store `boolean true` to `c`
  570. <5> allocate `Object` instance -> `Object reference`;
  571. store `Object reference` to `b`
  572. <6> load from `a` -> `Object reference`;
  573. load from `b` -> `null`;
  574. call `equals` on `Object reference` with arguments (`null`)
  575. -> `boolean false`;
  576. store `boolean false` to `c`
  577. +
  578. * Equality equals with the `def` type.
  579. +
  580. [source, Painless]
  581. ----
  582. <1> def a = 0;
  583. <2> def b = 1;
  584. <3> boolean c = a == b;
  585. <4> def d = new HashMap();
  586. <5> def e = new ArrayList();
  587. <6> c = d == e;
  588. ----
  589. +
  590. <1> declare `def a`;
  591. implicit cast `int 0` to `def` -> `def`;
  592. store `def` to `a`;
  593. <2> declare `def b`;
  594. implicit cast `int 1` to `def` -> `def`;
  595. store `def` to `b`;
  596. <3> declare `boolean c`;
  597. load from `a` -> `def`;
  598. implicit cast `a` to `int 0` -> `int 0`;
  599. load from `b` -> `def`;
  600. implicit cast `b` to `int 1` -> `int 1`;
  601. equality equals `int 0` and `int 1` -> `boolean false`;
  602. store `boolean false` to `c`
  603. <4> declare `def d`;
  604. allocate `HashMap` instance -> `HashMap reference`;
  605. implicit cast `HashMap reference` to `def` -> `def`
  606. store `def` to `d`;
  607. <5> declare `def e`;
  608. allocate `ArrayList` instance -> `ArrayList reference`;
  609. implicit cast `ArrayList reference` to `def` -> `def`
  610. store `def` to `d`;
  611. <6> load from `d` -> `def`;
  612. implicit cast `def` to `HashMap reference` -> `HashMap reference`;
  613. load from `e` -> `def`;
  614. implicit cast `def` to `ArrayList reference` -> `ArrayList reference`;
  615. call `equals` on `HashMap reference` with arguments (`ArrayList reference`)
  616. -> `boolean false`;
  617. store `boolean false` to `c`
  618. [[equality-not-equals-operator]]
  619. ==== Equality Not Equals
  620. Use the `equality not equals operator '!='` to COMPARE two values where a
  621. resultant `boolean` type value is `true` if the two values are NOT equal and
  622. `false` otherwise. The member method, `equals`, is implicitly called when the
  623. values are reference type values where the first value is the target of the call
  624. and the second value is the argument with the resultant `boolean` type value
  625. flipped. This operation is `null-safe` where if both values are `null` the
  626. resultant `boolean` type value is `false`, and if only one value is `null` the
  627. resultant `boolean` type value is `true`. A valid comparison is between boolean
  628. type values, numeric type values, or reference type values.
  629. *Errors*
  630. * If a comparison is made between a `boolean` type value and numeric type value.
  631. * If a comparison is made between a primitive type value and a reference type
  632. value.
  633. *Grammar*
  634. [source,ANTLR4]
  635. ----
  636. equality_not_equals: expression '!=' expression;
  637. ----
  638. *Promotion*
  639. [cols="<1,^1,^1,^1,^1,^1,^1,^1,^1,^1,^1"]
  640. |====
  641. | | boolean | byte | short | char | int | long | float | double | Reference | def
  642. | boolean | boolean | - | - | - | - | - | - | - | - | def
  643. | byte | - | int | int | int | int | long | float | double | - | def
  644. | short | - | int | int | int | int | long | float | double | - | def
  645. | char | - | int | int | int | int | long | float | double | - | def
  646. | int | - | int | int | int | int | long | float | double | - | def
  647. | long | - | long | long | long | long | long | float | double | - | def
  648. | float | - | float | float | float | float | float | float | double | - | def
  649. | double | - | double | double | double | double | double | double | double | - | def
  650. | Reference | - | - | - | - | - | - | - | - | Object | def
  651. | def | def | def | def | def | def | def | def | def | def | def
  652. |====
  653. *Examples*
  654. * Equality not equals with the `boolean` type.
  655. +
  656. [source,Painless]
  657. ----
  658. <1> boolean a = true;
  659. <2> boolean b = false;
  660. <3> a = a != false;
  661. <4> b = a != b;
  662. ----
  663. +
  664. <1> declare `boolean a`;
  665. store `boolean true` to `a`
  666. <2> declare `boolean b`;
  667. store `boolean false` to `b`
  668. <3> load from `a` -> `boolean true`;
  669. equality not equals `boolean true` and `boolean false` -> `boolean true`;
  670. store `boolean true` to `a`
  671. <4> load from `a` -> `boolean true`;
  672. load from `b` -> `boolean false`;
  673. equality not equals `boolean true` and `boolean false` -> `boolean true`;
  674. store `boolean true` to `b`
  675. +
  676. * Equality not equals with primitive types.
  677. +
  678. [source,Painless]
  679. ----
  680. <1> int a = 1;
  681. <2> double b = 2.0;
  682. <3> boolean c = a != b;
  683. <4> c = 1 != a;
  684. ----
  685. +
  686. <1> declare `int a`;
  687. store `int 1` to `a`
  688. <2> declare `double b`;
  689. store `double 1.0` to `b`
  690. <3> declare `boolean c`;
  691. load from `a` -> `int 1`;
  692. load from `b` -> `double 2.0`;
  693. promote `int 1` and `double 2.0`: result `double`;
  694. implicit cast `int 1` to `double 1.0` -> `double `1.0`;
  695. equality not equals `double 1.0` and `double 2.0` -> `boolean true`;
  696. store `boolean true` to `c`
  697. <4> load from `a` -> `int 1 @1`;
  698. equality not equals `int 1 @0` and `int 1 @1` -> `boolean false`;
  699. store `boolean false` to `c`
  700. +
  701. * Equality not equals with reference types.
  702. +
  703. [source,Painless]
  704. ----
  705. <1> List a = new ArrayList();
  706. <2> List b = new ArrayList();
  707. <3> a.add(1);
  708. <4> boolean c = a == b;
  709. <5> b.add(1);
  710. <6> c = a == b;
  711. ----
  712. +
  713. <1> declare `List a`;
  714. allocate `ArrayList` instance -> `ArrayList reference`;
  715. implicit cast `ArrayList reference` to `List reference` -> `List reference`;
  716. store `List reference` to `a`
  717. <2> declare `List b`;
  718. allocate `ArrayList` instance -> `ArrayList reference`;
  719. implicit cast `ArrayList reference` to `List reference` -> `List reference`;
  720. store `List reference` to `b`
  721. <3> load from `a` -> `List reference`;
  722. call `add` on `List reference` with arguments (`int 1)`
  723. <4> declare `boolean c`;
  724. load from `a` -> `List reference @0`;
  725. load from `b` -> `List reference @1`;
  726. call `equals` on `List reference @0` with arguments (`List reference @1`)
  727. -> `boolean false`;
  728. boolean not `boolean false` -> `boolean true`
  729. store `boolean true` to `c`
  730. <5> load from `b` -> `List reference`;
  731. call `add` on `List reference` with arguments (`int 1`)
  732. <6> load from `a` -> `List reference @0`;
  733. load from `b` -> `List reference @1`;
  734. call `equals` on `List reference @0` with arguments (`List reference @1`)
  735. -> `boolean true`;
  736. boolean not `boolean true` -> `boolean false`;
  737. store `boolean false` to `c`
  738. +
  739. * Equality not equals with `null`.
  740. +
  741. [source,Painless]
  742. ----
  743. <1> Object a = null;
  744. <2> Object b = null;
  745. <3> boolean c = a == null;
  746. <4> c = a == b;
  747. <5> b = new Object();
  748. <6> c = a == b;
  749. ----
  750. +
  751. <1> declare `Object a`;
  752. store `null` to `a`
  753. <2> declare `Object b`;
  754. store `null` to `b`
  755. <3> declare `boolean c`;
  756. load from `a` -> `null @0`;
  757. equality not equals `null @0` and `null @1` -> `boolean false`;
  758. store `boolean false` to `c`
  759. <4> load from `a` -> `null @0`;
  760. load from `b` -> `null @1`;
  761. equality not equals `null @0` and `null @1` -> `boolean false`;
  762. store `boolean false` to `c`
  763. <5> allocate `Object` instance -> `Object reference`;
  764. store `Object reference` to `b`
  765. <6> load from `a` -> `Object reference`;
  766. load from `b` -> `null`;
  767. call `equals` on `Object reference` with arguments (`null`)
  768. -> `boolean false`;
  769. boolean not `boolean false` -> `boolean true`;
  770. store `boolean true` to `c`
  771. +
  772. * Equality not equals with the `def` type.
  773. +
  774. [source, Painless]
  775. ----
  776. <1> def a = 0;
  777. <2> def b = 1;
  778. <3> boolean c = a == b;
  779. <4> def d = new HashMap();
  780. <5> def e = new ArrayList();
  781. <6> c = d == e;
  782. ----
  783. +
  784. <1> declare `def a`;
  785. implicit cast `int 0` to `def` -> `def`;
  786. store `def` to `a`;
  787. <2> declare `def b`;
  788. implicit cast `int 1` to `def` -> `def`;
  789. store `def` to `b`;
  790. <3> declare `boolean c`;
  791. load from `a` -> `def`;
  792. implicit cast `a` to `int 0` -> `int 0`;
  793. load from `b` -> `def`;
  794. implicit cast `b` to `int 1` -> `int 1`;
  795. equality equals `int 0` and `int 1` -> `boolean false`;
  796. store `boolean false` to `c`
  797. <4> declare `def d`;
  798. allocate `HashMap` instance -> `HashMap reference`;
  799. implicit cast `HashMap reference` to `def` -> `def`
  800. store `def` to `d`;
  801. <5> declare `def e`;
  802. allocate `ArrayList` instance -> `ArrayList reference`;
  803. implicit cast `ArrayList reference` to `def` -> `def`
  804. store `def` to `d`;
  805. <6> load from `d` -> `def`;
  806. implicit cast `def` to `HashMap reference` -> `HashMap reference`;
  807. load from `e` -> `def`;
  808. implicit cast `def` to `ArrayList reference` -> `ArrayList reference`;
  809. call `equals` on `HashMap reference` with arguments (`ArrayList reference`)
  810. -> `boolean false`;
  811. store `boolean false` to `c`
  812. [[identity-equals-operator]]
  813. ==== Identity Equals
  814. Use the `identity equals operator '==='` to COMPARE two values where a resultant
  815. `boolean` type value is `true` if the two values are equal and `false`
  816. otherwise. A reference type value is equal to another reference type value if
  817. both values refer to same instance on the heap or if both values are `null`. A
  818. valid comparison is between `boolean` type values, numeric type values, or
  819. reference type values.
  820. *Errors*
  821. * If a comparison is made between a `boolean` type value and numeric type value.
  822. * If a comparison is made between a primitive type value and a reference type
  823. value.
  824. *Grammar*
  825. [source,ANTLR4]
  826. ----
  827. identity_equals: expression '===' expression;
  828. ----
  829. *Promotion*
  830. [cols="<1,^1,^1,^1,^1,^1,^1,^1,^1,^1,^1"]
  831. |====
  832. | | boolean | byte | short | char | int | long | float | double | Reference | def
  833. | boolean | boolean | - | - | - | - | - | - | - | - | def
  834. | byte | - | int | int | int | int | long | float | double | - | def
  835. | short | - | int | int | int | int | long | float | double | - | def
  836. | char | - | int | int | int | int | long | float | double | - | def
  837. | int | - | int | int | int | int | long | float | double | - | def
  838. | long | - | long | long | long | long | long | float | double | - | def
  839. | float | - | float | float | float | float | float | float | double | - | def
  840. | double | - | double | double | double | double | double | double | double | - | def
  841. | Reference | - | - | - | - | - | - | - | - | Object | def
  842. | def | def | def | def | def | def | def | def | def | def | def
  843. |====
  844. *Examples*
  845. * Identity equals with reference types.
  846. +
  847. [source,Painless]
  848. ----
  849. <1> List a = new ArrayList();
  850. <2> List b = new ArrayList();
  851. <3> List c = a;
  852. <4> boolean c = a === b;
  853. <5> c = a === c;
  854. ----
  855. +
  856. <1> declare `List a`;
  857. allocate `ArrayList` instance -> `ArrayList reference`;
  858. implicit cast `ArrayList reference` to `List reference` -> `List reference`;
  859. store `List reference` to `a`
  860. <2> declare `List b`;
  861. allocate `ArrayList` instance -> `ArrayList reference`;
  862. implicit cast `ArrayList reference` to `List reference` -> `List reference`;
  863. store `List reference` to `b`
  864. <3> load from `a` -> `List reference`;
  865. store `List reference` to `c`
  866. <4> declare `boolean c`;
  867. load from `a` -> `List reference @0`;
  868. load from `b` -> `List reference @1`;
  869. identity equals `List reference @0` and `List reference @1`
  870. -> `boolean false`
  871. store `boolean false` to `c`
  872. <5> load from `a` -> `List reference @0`;
  873. load from `c` -> `List reference @1`;
  874. identity equals `List reference @0` and `List reference @1`
  875. -> `boolean true`
  876. store `boolean true` to `c`
  877. (note `List reference @0` and `List reference @1` refer to the same
  878. instance)
  879. +
  880. * Identity equals with `null`.
  881. +
  882. [source,Painless]
  883. ----
  884. <1> Object a = null;
  885. <2> Object b = null;
  886. <3> boolean c = a === null;
  887. <4> c = a === b;
  888. <5> b = new Object();
  889. <6> c = a === b;
  890. ----
  891. +
  892. <1> declare `Object a`;
  893. store `null` to `a`
  894. <2> declare `Object b`;
  895. store `null` to `b`
  896. <3> declare `boolean c`;
  897. load from `a` -> `null @0`;
  898. identity equals `null @0` and `null @1` -> `boolean true`;
  899. store `boolean true` to `c`
  900. <4> load from `a` -> `null @0`;
  901. load from `b` -> `null @1`;
  902. identity equals `null @0` and `null @1` -> `boolean true`;
  903. store `boolean true` to `c`
  904. <5> allocate `Object` instance -> `Object reference`;
  905. store `Object reference` to `b`
  906. <6> load from `a` -> `Object reference`;
  907. load from `b` -> `null`;
  908. identity equals `Object reference` and `null` -> `boolean false`;
  909. store `boolean false` to `c`
  910. +
  911. * Identity equals with the `def` type.
  912. +
  913. [source, Painless]
  914. ----
  915. <1> def a = new HashMap();
  916. <2> def b = new ArrayList();
  917. <3> boolean c = a === b;
  918. <4> b = a;
  919. <5> c = a === b;
  920. ----
  921. +
  922. <1> declare `def d`;
  923. allocate `HashMap` instance -> `HashMap reference`;
  924. implicit cast `HashMap reference` to `def` -> `def`
  925. store `def` to `d`
  926. <2> declare `def e`;
  927. allocate `ArrayList` instance -> `ArrayList reference`;
  928. implicit cast `ArrayList reference` to `def` -> `def`
  929. store `def` to `d`
  930. <3> declare `boolean c`;
  931. load from `a` -> `def`;
  932. implicit cast `def` to `HashMap reference` -> `HashMap reference`;
  933. load from `b` -> `def`;
  934. implicit cast `def` to `ArrayList reference` -> `ArrayList reference`;
  935. identity equals `HashMap reference` and `ArrayList reference`
  936. -> `boolean false`;
  937. store `boolean false` to `c`
  938. <4> load from `a` -> `def`;
  939. store `def` to `b`
  940. <5> load from `a` -> `def`;
  941. implicit cast `def` to `HashMap reference @0` -> `HashMap reference @0`;
  942. load from `b` -> `def`;
  943. implicit cast `def` to `HashMap reference @1` -> `HashMap reference @1`;
  944. identity equals `HashMap reference @0` and `HashMap reference @1`
  945. -> `boolean true`;
  946. store `boolean true` to `b`;
  947. (note `HashMap reference @0` and `HashMap reference @1` refer to the same
  948. instance)
  949. [[identity-not-equals-operator]]
  950. ==== Identity Not Equals
  951. Use the `identity not equals operator '!=='` to COMPARE two values where a
  952. resultant `boolean` type value is `true` if the two values are NOT equal and
  953. `false` otherwise. A reference type value is not equal to another reference type
  954. value if both values refer to different instances on the heap or if one value is
  955. `null` and the other is not. A valid comparison is between `boolean` type
  956. values, numeric type values, or reference type values.
  957. *Errors*
  958. * If a comparison is made between a `boolean` type value and numeric type value.
  959. * If a comparison is made between a primitive type value and a reference type
  960. value.
  961. *Grammar*
  962. [source,ANTLR4]
  963. ----
  964. identity_not_equals: expression '!==' expression;
  965. ----
  966. *Promotion*
  967. [cols="<1,^1,^1,^1,^1,^1,^1,^1,^1,^1,^1"]
  968. |====
  969. | | boolean | byte | short | char | int | long | float | double | Reference | def
  970. | boolean | boolean | - | - | - | - | - | - | - | - | def
  971. | byte | - | int | int | int | int | long | float | double | - | def
  972. | short | - | int | int | int | int | long | float | double | - | def
  973. | char | - | int | int | int | int | long | float | double | - | def
  974. | int | - | int | int | int | int | long | float | double | - | def
  975. | long | - | long | long | long | long | long | float | double | - | def
  976. | float | - | float | float | float | float | float | float | double | - | def
  977. | double | - | double | double | double | double | double | double | double | - | def
  978. | Reference | - | - | - | - | - | - | - | - | Object | def
  979. | def | def | def | def | def | def | def | def | def | def | def
  980. |====
  981. *Examples*
  982. * Identity not equals with reference type values.
  983. +
  984. [source,Painless]
  985. ----
  986. <1> List a = new ArrayList();
  987. <2> List b = new ArrayList();
  988. <3> List c = a;
  989. <4> boolean c = a !== b;
  990. <5> c = a !== c;
  991. ----
  992. +
  993. <1> declare `List a`;
  994. allocate `ArrayList` instance -> `ArrayList reference`;
  995. implicit cast `ArrayList reference` to `List reference` -> `List reference`;
  996. store `List reference` to `a`
  997. <2> declare `List b`;
  998. allocate `ArrayList` instance -> `ArrayList reference`;
  999. implicit cast `ArrayList reference` to `List reference` -> `List reference`;
  1000. store `List reference` to `b`
  1001. <3> load from `a` -> `List reference`;
  1002. store `List reference` to `c`
  1003. <4> declare `boolean c`;
  1004. load from `a` -> `List reference @0`;
  1005. load from `b` -> `List reference @1`;
  1006. identity not equals `List reference @0` and `List reference @1`
  1007. -> `boolean true`
  1008. store `boolean true` to `c`
  1009. <5> load from `a` -> `List reference @0`;
  1010. load from `c` -> `List reference @1`;
  1011. identity not equals `List reference @0` and `List reference @1`
  1012. -> `boolean false`
  1013. store `boolean false` to `c`
  1014. (note `List reference @0` and `List reference @1` refer to the same
  1015. instance)
  1016. +
  1017. * Identity not equals with `null`.
  1018. +
  1019. [source,Painless]
  1020. ----
  1021. <1> Object a = null;
  1022. <2> Object b = null;
  1023. <3> boolean c = a !== null;
  1024. <4> c = a !== b;
  1025. <5> b = new Object();
  1026. <6> c = a !== b;
  1027. ----
  1028. +
  1029. <1> declare `Object a`;
  1030. store `null` to `a`
  1031. <2> declare `Object b`;
  1032. store `null` to `b`
  1033. <3> declare `boolean c`;
  1034. load from `a` -> `null @0`;
  1035. identity not equals `null @0` and `null @1` -> `boolean false`;
  1036. store `boolean false` to `c`
  1037. <4> load from `a` -> `null @0`;
  1038. load from `b` -> `null @1`;
  1039. identity not equals `null @0` and `null @1` -> `boolean false`;
  1040. store `boolean false` to `c`
  1041. <5> allocate `Object` instance -> `Object reference`;
  1042. store `Object reference` to `b`
  1043. <6> load from `a` -> `Object reference`;
  1044. load from `b` -> `null`;
  1045. identity not equals `Object reference` and `null` -> `boolean true`;
  1046. store `boolean true` to `c`
  1047. +
  1048. * Identity not equals with the `def` type.
  1049. +
  1050. [source, Painless]
  1051. ----
  1052. <1> def a = new HashMap();
  1053. <2> def b = new ArrayList();
  1054. <3> boolean c = a !== b;
  1055. <4> b = a;
  1056. <5> c = a !== b;
  1057. ----
  1058. +
  1059. <1> declare `def d`;
  1060. allocate `HashMap` instance -> `HashMap reference`;
  1061. implicit cast `HashMap reference` to `def` -> `def`
  1062. store `def` to `d`
  1063. <2> declare `def e`;
  1064. allocate `ArrayList` instance -> `ArrayList reference`;
  1065. implicit cast `ArrayList reference` to `def` -> `def`
  1066. store `def` to `d`
  1067. <3> declare `boolean c`;
  1068. load from `a` -> `def`;
  1069. implicit cast `def` to `HashMap reference` -> `HashMap reference`;
  1070. load from `b` -> `def`;
  1071. implicit cast `def` to `ArrayList reference` -> `ArrayList reference`;
  1072. identity not equals `HashMap reference` and `ArrayList reference`
  1073. -> `boolean true`;
  1074. store `boolean true` to `c`
  1075. <4> load from `a` -> `def`;
  1076. store `def` to `b`
  1077. <5> load from `a` -> `def`;
  1078. implicit cast `def` to `HashMap reference @0` -> `HashMap reference @0`;
  1079. load from `b` -> `def`;
  1080. implicit cast `def` to `HashMap reference @1` -> `HashMap reference @1`;
  1081. identity not equals `HashMap reference @0` and `HashMap reference @1`
  1082. -> `boolean false`;
  1083. store `boolean false` to `b`;
  1084. (note `HashMap reference @0` and `HashMap reference @1` refer to the same
  1085. instance)
  1086. [[boolean-xor-operator]]
  1087. ==== Boolean Xor
  1088. Use the `boolean xor operator '^'` to XOR together two `boolean` type values
  1089. where if one `boolean` type value is `true` and the other is `false` the
  1090. resultant `boolean` type value is `true` and `false` otherwise.
  1091. *Errors*
  1092. * If either evaluated value is a value other than a `boolean` type value or
  1093. a value that is castable to a `boolean` type value.
  1094. *Truth*
  1095. [cols="^1,^1,^1"]
  1096. |====
  1097. | | true | false
  1098. | true | false | true
  1099. | false | true | false
  1100. |====
  1101. *Grammar*
  1102. [source,ANTLR4]
  1103. ----
  1104. boolean_xor: expression '^' expression;
  1105. ----
  1106. *Examples*
  1107. * Boolean xor with the `boolean` type.
  1108. +
  1109. [source,Painless]
  1110. ----
  1111. <1> boolean x = false;
  1112. <2> boolean y = x ^ true;
  1113. <3> y = y ^ x;
  1114. ----
  1115. +
  1116. <1> declare `boolean x`;
  1117. store `boolean false` to `x`
  1118. <2> declare `boolean y`;
  1119. load from `x` -> `boolean false`
  1120. boolean xor `boolean false` and `boolean true` -> `boolean true`;
  1121. store `boolean true` to `y`
  1122. <3> load from `y` -> `boolean true @0`;
  1123. load from `x` -> `boolean true @1`;
  1124. boolean xor `boolean true @0` and `boolean true @1` -> `boolean false`;
  1125. store `boolean false` to `y`
  1126. +
  1127. * Boolean xor with the `def` type.
  1128. +
  1129. [source,Painless]
  1130. ----
  1131. <1> def x = false;
  1132. <2> def y = x ^ true;
  1133. <3> y = y ^ x;
  1134. ----
  1135. +
  1136. <1> declare `def x`;
  1137. implicit cast `boolean false` to `def` -> `def`;
  1138. store `def` to `x`
  1139. <2> declare `def y`;
  1140. load from `x` -> `def`;
  1141. implicit cast `def` to `boolean false` -> `boolean false`;
  1142. boolean xor `boolean false` and `boolean true` -> `boolean true`;
  1143. implicit cast `boolean true` to `def` -> `def`;
  1144. store `def` to `y`
  1145. <3> load from `y` -> `def`;
  1146. implicit cast `def` to `boolean true @0` -> `boolean true @0`;
  1147. load from `x` -> `def`;
  1148. implicit cast `def` to `boolean true @1` -> `boolean true @1`;
  1149. boolean xor `boolean true @0` and `boolean true @1` -> `boolean false`;
  1150. implicit cast `boolean false` -> `def`;
  1151. store `def` to `y`
  1152. [[boolean-and-operator]]
  1153. ==== Boolean And
  1154. Use the `boolean and operator '&&'` to AND together two `boolean` type values
  1155. where if both `boolean` type values are `true` the resultant `boolean` type
  1156. value is `true` and `false` otherwise.
  1157. *Errors*
  1158. * If either evaluated value is a value other than a `boolean` type value or
  1159. a value that is castable to a `boolean` type value.
  1160. *Truth*
  1161. [cols="^1,^1,^1"]
  1162. |====
  1163. | | true | false
  1164. | true | true | false
  1165. | false | false | false
  1166. |====
  1167. *Grammar*
  1168. [source,ANTLR4]
  1169. ----
  1170. boolean_and: expression '&&' expression;
  1171. ----
  1172. *Examples*
  1173. * Boolean and with the `boolean` type.
  1174. +
  1175. [source,Painless]
  1176. ----
  1177. <1> boolean x = true;
  1178. <2> boolean y = x && true;
  1179. <3> x = false;
  1180. <4> y = y && x;
  1181. ----
  1182. +
  1183. <1> declare `boolean x`;
  1184. store `boolean true` to `x`
  1185. <2> declare `boolean y`;
  1186. load from `x` -> `boolean true @0`;
  1187. boolean and `boolean true @0` and `boolean true @1` -> `boolean true`;
  1188. store `boolean true` to `y`
  1189. <3> store `boolean false` to `x`
  1190. <4> load from `y` -> `boolean true`;
  1191. load from `x` -> `boolean false`;
  1192. boolean and `boolean true` and `boolean false` -> `boolean false`;
  1193. store `boolean false` to `y`
  1194. +
  1195. * Boolean and with the `def` type.
  1196. +
  1197. [source,Painless]
  1198. ----
  1199. <1> def x = true;
  1200. <2> def y = x && true;
  1201. <3> x = false;
  1202. <4> y = y && x;
  1203. ----
  1204. +
  1205. <1> declare `def x`;
  1206. implicit cast `boolean true` to `def` -> `def`;
  1207. store `def` to `x`
  1208. <2> declare `def y`;
  1209. load from `x` -> `def`;
  1210. implicit cast `def` to `boolean true @0` -> `boolean true @0`;
  1211. boolean and `boolean true @0` and `boolean true @1` -> `boolean true`;
  1212. implicit cast `boolean true` to `def` -> `def`;
  1213. store `def` to `y`
  1214. <3> implicit cast `boolean false` to `def` -> `def`;
  1215. store `def` to `x`;
  1216. <4> load from `y` -> `def`;
  1217. implicit cast `def` to `boolean true` -> `boolean true`;
  1218. load from `x` -> `def`;
  1219. implicit cast `def` to `boolean false` -> `boolean false`;
  1220. boolean and `boolean true` and `boolean false` -> `boolean false`;
  1221. implicit cast `boolean false` -> `def`;
  1222. store `def` to `y`
  1223. [[boolean-or-operator]]
  1224. ==== Boolean Or
  1225. Use the `boolean or operator '||'` to OR together two `boolean` type values
  1226. where if either one of the `boolean` type values is `true` the resultant
  1227. `boolean` type value is `true` and `false` otherwise.
  1228. *Errors*
  1229. * If either evaluated value is a value other than a `boolean` type value or
  1230. a value that is castable to a `boolean` type value.
  1231. *Truth*
  1232. [cols="^1,^1,^1"]
  1233. |====
  1234. | | true | false
  1235. | true | true | true
  1236. | false | true | false
  1237. |====
  1238. *Grammar:*
  1239. [source,ANTLR4]
  1240. ----
  1241. boolean_and: expression '||' expression;
  1242. ----
  1243. *Examples*
  1244. * Boolean or with the `boolean` type.
  1245. +
  1246. [source,Painless]
  1247. ----
  1248. <1> boolean x = false;
  1249. <2> boolean y = x || true;
  1250. <3> y = false;
  1251. <4> y = y || x;
  1252. ----
  1253. +
  1254. <1> declare `boolean x`;
  1255. store `boolean false` to `x`
  1256. <2> declare `boolean y`;
  1257. load from `x` -> `boolean false`;
  1258. boolean or `boolean false` and `boolean true` -> `boolean true`;
  1259. store `boolean true` to `y`
  1260. <3> store `boolean false` to `y`
  1261. <4> load from `y` -> `boolean false @0`;
  1262. load from `x` -> `boolean false @1`;
  1263. boolean or `boolean false @0` and `boolean false @1` -> `boolean false`;
  1264. store `boolean false` to `y`
  1265. +
  1266. * Boolean or with the `def` type.
  1267. +
  1268. [source,Painless]
  1269. ----
  1270. <1> def x = false;
  1271. <2> def y = x || true;
  1272. <3> y = false;
  1273. <4> y = y || x;
  1274. ----
  1275. +
  1276. <1> declare `def x`;
  1277. implicit cast `boolean false` to `def` -> `def`;
  1278. store `def` to `x`
  1279. <2> declare `def y`;
  1280. load from `x` -> `def`;
  1281. implicit cast `def` to `boolean false` -> `boolean true`;
  1282. boolean or `boolean false` and `boolean true` -> `boolean true`;
  1283. implicit cast `boolean true` to `def` -> `def`;
  1284. store `def` to `y`
  1285. <3> implicit cast `boolean false` to `def` -> `def`;
  1286. store `def` to `y`;
  1287. <4> load from `y` -> `def`;
  1288. implicit cast `def` to `boolean false @0` -> `boolean false @0`;
  1289. load from `x` -> `def`;
  1290. implicit cast `def` to `boolean false @1` -> `boolean false @1`;
  1291. boolean or `boolean false @0` and `boolean false @1` -> `boolean false`;
  1292. implicit cast `boolean false` -> `def`;
  1293. store `def` to `y`