Generic Top

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:


1. Introduction

In this quick tutorial, we're going to explore different ways to find the data type in Groovy.

Actually, it's different depending on what we're doing:

  • First, we'll look at what to do for primitives
  • Then, we'll see how collections bring some unique challenges
  • And finally, we'll look at objects and class variables

2. Primitive Types

Groovy supports the same number of primitive types as Java. We can find the data type of primitives in three ways.

To begin, let's imagine we have multiple representations of a person's age.

First of all, let's start with the instanceof operator:

public void givenWhenParameterTypeIsInteger_thenReturnTrue() {
    Person personObj = new Person(10)
    Assert.assertTrue(personObj.ageAsInt instanceof Integer);

instanceof is a binary operator that we can use to check if an object is an instance of a given type. It returns true if the object is an instance of that particular type and false otherwise.

Also, Groovy 3 adds the new !instanceof operator. It returns true if the object is not an instance of a type and false otherwise.

Then, we can also use the getClass() method from the Object class. It returns the runtime class of an instance:

public void givenWhenParameterTypeIsDouble_thenReturnTrue() {
    Person personObj = new Person(10.0)
    Assert.assertTrue((personObj.ageAsDouble).getClass() == Double)

Lastly, let's apply the .class operator to find the data type:

public void givenWhenParameterTypeIsString_thenReturnTrue() {
    Person personObj = new Person("10 years")
    Assert.assertTrue(personObj.ageAsString.class == String)

Similarly, we can find the data type of any primitive type.

3. Collections

Groovy provides support for various collection types.

Let's define a simple list in Groovy:

public void givenGroovyList_WhenFindClassName_thenReturnTrue() {
    def ageList = ['ageAsString','ageAsDouble', 10]
    Assert.assertTrue(ageList.class == ArrayList)
    Assert.assertTrue(ageList.getClass() == ArrayList)

But on maps, the .class operator cannot be applied:

public void givenGrooyMap_WhenFindClassName_thenReturnTrue() {
    def ageMap = [ageAsString: '10 years', ageAsDouble: 10.0]
    Assert.assertFalse(ageMap.class == LinkedHashMap)

In the above code snippet, ageMap.class will try to retrieve the value of the key class from the given map. For maps, it is recommended to apply getClass() than .class.

4. Objects & Class Variables

In the above sections, we used various strategies to find the data type of primitives and collections.

To see how class variables work, let’s suppose we have a class Person:

public void givenClassName_WhenParameterIsInteger_thenReturnTrue() {
    Assert.assertTrue(Person.class.getDeclaredField('ageAsInt').type == int.class)

Remember that the getDeclaredField() returns all the fields of a certain class.

We can find the type of any object using instanceof, getClass() and .class operators:

public void givenWhenObjectIsInstanceOfType_thenReturnTrue() {
    Person personObj = new Person()
    Assert.assertTrue(personObj instanceof Person)

Moreover, we can also use the Groovy membership operator in:

public void givenWhenInstanceIsOfSubtype_thenReturnTrue() {
    Student studentObj = new Student()
    Assert.assertTrue(studentObj in Person)

5. Conclusion

In this short article, we saw how to find the data type in Groovy. By comparison, the getClass() method is safer than .class operator. We also discussed the working of in operator along with instanceof operator. Additionally, we learned how to get all the fields of a class and apply the .type operator.

As always, all the code snippets can be found over on GitHub.

Generic bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

Generic footer banner
Comments are closed on this article!