Values in Groovy
Groovy has various value types including strings, integers, floats, booleans, etc. Here are a few basic examples.
// Strings, which can be added together with +.
println "groovy" + "lang"
// Integers and floats.
println "1+1 = ${1+1}"
println "7.0/3.0 = ${7.0/3.0}"
// Booleans, with boolean operators as you'd expect.
println true && false
println true || false
println !true
To run this Groovy script, save it in a file (e.g., values.groovy
) and execute it using the groovy
command:
$ groovy values.groovy
groovylang
1+1 = 2
7.0/3.0 = 2.3333333333333335
false
true
false
In Groovy, we don’t need to explicitly declare a main
method or import any packages for basic printing. The println
function is available by default.
Groovy also supports string interpolation using ${expression}
syntax within double-quoted strings, which we’ve used for the arithmetic operations.
The boolean operations work the same way as in many other programming languages, including the use of &&
for logical AND, ||
for logical OR, and !
for logical NOT.