The precedence hierarchy of the operators, some of which we haven't looked at yet, is, from highest to lowest:
$(scope escape)
new ()(parentheses)
[](subscripting) ()(method call) {}(closable block) [](list/map)
. ?. *. (dots)
~ ! $ ()(cast type)
**(power)
++(pre/post) --(pre/post) +(unary) -(unary)
* / %
+(binary) -(binary)
<< >> >>> .. ..<
< <= > >= instanceof in as
== != <=>
&
^
|
&&
||
?:
= **= *= /= %= += -= <<= >>= >>>= &= ^= |=
Aún no hemos estudiado todos los operadores, como es el caso de *.
.
Veamos un ejemplo de uso:
generaciondecodigos@nereida:~/Lgroovy/collections$ cat -n spreadDotOperator.groovy 1 class Language { 2 String lang 3 def speak() { "$lang speaks." } 4 } 5 6 // Create a list with 3 objects. Each object has a lang 7 // property and a speak() method. 8 def list = [ 9 new Language(lang: 'Groovy'), 10 new Language(lang: 'Java'), 11 new Language(lang: 'Scala') 12 ] 13 14 // Use the spread-dot operator to invoke the speak() method. 15 println list*.speak() 16 println list.collect{ it.speak() } 17 18 // We can also use the spread-dot operator to access 19 // properties, but we don't need to, because Groovy allows 20 // direct property access on list members. 21 println list*.lang 22 println list.langAl ejecutarse, este programa produce la siguiente salida:
generaciondecodigos@nereida:~/Lgroovy/collections$ groovy spreadDotOperator.groovy [Groovy speaks., Java speaks., Scala speaks.] [Groovy speaks., Java speaks., Scala speaks.] [Groovy, Java, Scala] [Groovy, Java, Scala]
Casiano Rodríguez León