5.1 Literals and Expressions

We can create literals in Groovy using single quotes—like ​’hello’​. In Java, ​’a’​ is a ​char​, while ​"a"​ is a ​String​. Groovy makes no such distinction; both of these are instances of ​String​ in Groovy. If we want to explicitly create a character, we simply type ​’a’ as char​. Of course, Groovy may implicitly create ​Character​ objects if any method calls demand it.

Groovy is also flexible about what we can put into a literal. For example, we can have double quotes in our string if we want:

WorkingWithStrings/Literals.groovy
 
println ​'He said, "That is Groovy"'

Groovy handled that fairly well, as we can see in the output:

 
He said, "That is Groovy"

Let’s examine the type of the object that was created ...

Get Programming Groovy 2 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.