Data types in Groovy

This guide discusses data types in Groovy and how they apply to Deephaven. Properly managing data types in queries leads to cleaner, faster, and more reusable code.

Groovy data types

Groovy uses Java’s underlying data types. Here are some of the common ones:

The data type of an integer literal is inferred from its size:

As in Java, you can define explicit types for variables:

Variables defined this way are not available in the query scope:

Boxed vs primitive

Variables in the Groovy binding are always boxed. Same with variables defined with def or var:

Square brackets create a Java List. Anything inside the brackets is, therefore, a Java Object:

You can convert a List to an array by casting, or with the as operator:

For more information on data types in Groovy, see the official Groovy documentation

Data types in closures

Deephaven tables don't know the return type of a Groovy closure unless you explicitly typecast the result:

The type of the column Z is a java.lang.Object. To get the data type you want, cast the result inside the query string:

Casting makes it easier to use the column in downstream queries. It can also improve memory usage by letting Deephaven know exactly how much memory to allocate to each cell.

Performance

It is generally a good idea to declare the data types of the input parameters to a closure if you know them ahead of time.

In xPlusY2, the data types of x and y are unknown at compile time. Groovy must determine what they are at runtime and what "plus" should mean for those two arguments. If x and y are numbers, it adds them; if x or y is a String, it concatenates them. Checking the data types and determining how to handle them can be much slower than the addition or concatenation itself. Static typing allows the Groovy compiler to best optimize your closure.