Groovy Basic Examples
1) String Usage
String usage in Groovy is similar to many programming languages, but Groovy has some unique features. Here are the basics of String usage:
String s1 = 'Single-quoted string'
String s2 = "Double-quoted string"
String s3 = '''Triple-quoted
multi-line
string'''
In Groovy, you can directly add variable values in double-quoted strings. This feature is called "String interpolation" or "GString".
String ad = "Ali"
String selam = "Merhaba, ${ad}!"
println(selam) // Output: Merhaba, Ali!
2) Differences Between Single, Double, and Triple Quote Usage in Strings
In Groovy, you can use single quote ('), double quote ("), and triple quote (''' or """) to define String. The differences in their usage are:
Single Quote (' '):
- Single-quoted strings are plain strings.
- String interpolation (variable insertion) cannot be done within them.
String s1 = 'Merhaba, ${isim}!'
println(s1) // Output: Merhaba, ${isim}!
Double Quote ("):
- Double-quoted strings are known as "GString".
- String interpolation (variable insertion) can be done within them.
String isim = "Ali"
String s2 = "Merhaba, ${isim}!"
println(s2) // Output: Merhaba, Ali!
Triple Quote('''):
- Used to define multi-line strings.
- String interpolation can be done within them.
String isim = "Ali"
String s3 = '''Merhaba,
${isim}!
Nasılsın?'''
println(s3)
// Output:
// Merhaba,
// Ali!
// Nasılsın?
3) Checking if String is Empty in Groovy
In Groovy, you can use the isEmpty() method to check if a string is empty.
String s = ""
if(s.isEmpty()) {
println("String is empty!")
} else {
println("String is not empty!")
}
4) Checking if Object is Empty in Groovy
In Groovy, you can use == null or != null expressions to check if an object is empty.
def obj = null
if(obj) {
println("Object is not empty!")
} else {
println("Object is empty!")
}
Output: "Object is empty!"
5) For Loop in Groovy
A for loop in Groovy is written as in Java. A basic example:
def toplam = 0
for(int i = 0; i < 10; i++) {
toplam +=i
responseBodyTextToClient = "toplam: ${toplam}"
}
//Output : "toplam: 45"
6) While Loop in Groovy
The while loop in Groovy is used as in other programming languages. Here is a basic example:
int sayi = 0
while(sayi < 10) {
sayi++
}
responseBodyTextToClient = sayi
//Output: "10"
7) Function (Method) Definition in Groovy
In Groovy, functions are usually called "methods" and are defined similar to Java. However, Groovy offers a more flexible syntax.
def fonksiyonAdi(parametre1, parametre2, ...) {
// Function content
return sonuc
}
def topla(a, b) {
return a + b
}
//After defining the function, you can call it by name to use it.
def sonuc = topla(5, 3)
responseBodyTextToClient =sonuc // Output: 8
8) Try-Catch Usage in Groovy
The try-catch structure in Groovy is used for catching and handling exceptions, similar to Java. The syntax and usage are very similar to Java.
try {
// Code that may cause errors
int sonuc = 10 / 0 // This line will produce an ArithmeticException error.
} catch (ArithmeticException e) {
responseBodyTextToClient ="An arithmetic error occurred: " + e.getMessage()
} catch (Exception e) {
responseBodyTextToClient ="An error occurred: " + e.getMessage()
}
//Output : "An arithmetic error occurred: divide by zero"
9) Concatenation in Groovy
There are several methods for string concatenation (concatenation) in Groovy. Here are some examples of these methods:
1. Simple Concatenation with + Operator:
String ad = "Ali"
String mesaj = "Merhaba, " + ad + "!"
2. GString (Parameterized String) Usage:
Groovy supports GString, a string type that can use the ${...} structure to directly add values into strings.
String ad = "Ali"
String mesaj = "Merhaba, ${ad}!"
println(mesaj) // Output: Merhaba, Ali!
3. Concatenation with << Operator:
This method is more performant for string concatenation operations on large data sets.
def sb = new StringBuilder()
sb << "Merhaba, "
sb << "Ali"
sb << "!"
println(sb.toString()) // Output: Merhaba, Ali!