In Teneo Studio the scripts are written in Groovy , which is a dynamic programming language and easier-to-learn than Java. Although you can use Java code in Teneo Studio in most of the cases , we encourage you to use Groovy which will make your code shorter and more readable. If you are a skilled Java developer but do not have much experience in Groovy, this post can give you some ideas about the syntax sugar Groovy provides.
Default imports
In Groovy, some of the commonly used packages and classes are imported by default, i.e. you do not have to use an explicit import statement to use them:
- java.io.*
- java.lang.*
- java.math.BigDecimal
- java.math.BigInteger
- java.net.*
- java.util.*
- groovy.lang.*
- groovy.util.*
Omit the semicolons
In Java you must use a semicolon at the end of each line, while in Groovy the semicolon is optional. We suggest you to use semicolons only when you need to separate between statements within one line of code, e.g. for (int i = 0; i < myList.size(); i++)
Easy syntax for print() and println()
Print and Println are used to display a text on the console, which are probably most used methods in programming. In Java it is obligatory to use System.out.print()
and System.out.println()
, while in Groovy you can simply use print()
and println()
. The parentheses are optional, so both of the following code will work: print("Message")
and print "Message"
.
Define variables with implicit type
When you are defining a variable in Java, you should define explicitly the data type such as String name = "Tommy"
. While in Groovy, you can define a variable without defining explicitly the data type by using the def keyword. The data type will be determined by the value of the variable, see the examples below:
These variables defined with implicit type can be easily switched to another data type by assigning a new value, for example:
Define array with square brackets
In Java you define an array using curly brackets, while in Groovy you need to use square brackets, for example int[] array = [1,2,3]
. The long syntax for array in Java like int[] array = new int[]{1,2,3}
is still valid in Groovy, but the short syntax like int[] array = {1,2,3}
is not valid. If you want to copy your Java code into Teneo Studio, you need to replace these curly brackets with square brackets in array creation.
Alternatives for 3-statement For loops
Although the standard 3-statement For loop in Java is still valid in Groovy, Groovy provides you with many simpler alternatives. For example, you have the following loop in Java:
for(int i = 0; i <= 5, i++){
System.out.print(i)
}
Below are four alternatives which will generate the same result as the 3-statement loop above:
- Use groovy.lang.IntRange class, which is written as x…y, representing from x to y with both x and y included:
for(i in 0..5){ print(i) }
- Use the upto method of Integer written as x.upto(y), where x and y are both integers and y must be greater than x. It will loop from x to y with both x and y included, for example:
0.upto(5){ print(it) }
- Use the times method of Integer written as x.times(), where x should be an integer greater than 0. It will loop x times (i.e. from 0 to x-1), for example:
6.times(){ print(it) }
- Use the step method of Integer written as x.step(y, z), where x, y and z are integers. It will loop from x to y with y excluded, taking a step of z, for example:
0.step(6,1){ print(it) }
Simple For-each loops
A For-each loop is a popular traversing technique to iterate over an array or arraylist. The following is a standard For-each loop in Java:
for (type var: array){
statements with var;
}
While in Groovy, you can either use the same expressions or in a simpler, Groovy Syntax Sugar, way:
for (var in array){
statements with var
}
Conclusion
This post provides you some examples of syntax sugar that Groovy provides compared to Java . You can find more differences between Java and Groovy here. Hope it can help when you are writing code in Teneo Studio!