O'Reilly logo

Programming Groovy 2 by Venkat Subramaniam

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 using the single quotes: ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required