Learn Java for Web Development

(Tina Meador) #1
APPENDIX B: Introduction to Groovy 409

This time Groovy does not interpret $ test in line 6 as an expression because there is an empty
space between $ and test, and it renders the output as follows:


Hello vishal
path= c:/groovy
dollar = $ test
path = c:/groovy


With a dollar slashy string, you are no longer required to escape the slash with a preceding
backslash (multiline slashy strings require the slash to be escaped), and you can use $$ to escape a
$ or use $/ to escape a slash if needed, as illustrated in Listing B-16.


Listing B-16. Using Dollar Slashy String



  1. def name = "vishal"

  2. def path= "c:/groovy"

  3. def dollarSlashy = $/

  4. Hello $name

  5. path = $path

  6. dollar = $$test

  7. path = c:/groovy

  8. /$

  9. println dollarSlashy


Hello vishal
path= c:/groovy
dollar = $test
path = c:/groovy


Let’s take a look at Listing B-16 in more detail.


   Line 3 defines a dollarSlashy string that includes up to line 8.
 Line 6 has a $test, which has caused a MissingPropertyException in the case
of the multiline slashy string in Listing B-14, which you now escape using a $.

Collective Datatypes


Groovy supports a number of different collections, including arrays, lists, maps, ranges, and sets.
Let’s look at how to create and use each of the collection types.

Free download pdf