coderolls Staff
coderolls Staff Hi 👋🏼, At coderolls we write tutorials about Java programming language and related technologies.

Groovy: How To Determine The Datatype Of An Object?

In this quick tutorial, we will see how we can determine the datatype of an object in Groovy.

Introduction

When you are writing a Groovy code, sometimes you need to check the datatype of an object. Also, not following the proper naming convention may lead to object datatype confusion.

We will see the best way to check the datatype of an object in Groovy so that you can use it correctly in the code.

How To Determine The Datatype Of An Object In Groovy?

To determine the class of an object, you can simply call the getClass() method on it.

An example is given below.

1
anObject.getClass()

let’s check the above-line program.

1
2
3
4
5
6
7
8
9
def names = ['Gaurav', 'Shubham', 'Nayan', 'Sudeep'];

println names.getClass(); // prints 'class java.util.ArrayList'

def blogname = '''coderolls''';

println blogname.getClass(); // prints 'class java.lang.String'

println blogname.getClass().name; // prints 'java.lang.String'

Output:

1
2
3
class java.util.ArrayList
class java.lang.String
java.lang.String

See above code as GitHub Gist

In the above program, we can see that the names is an ArrayList and blogname is a String object.

When we call the getClass() method on these objects it prints its respective datatype.

In most of the cases, you can use the anObject.getClass() as anObject.class. But if anObject is Map it will try to retrieve the value with key ‘class’. Because of this, it is always recommended to use anObject.getClass() instead of anObject.class.

If you want to check if an object implements a particular interface or extends a particular class (e.g. String, ArrayList, etc), you can use the instanceof keyword.

For example,

1
(anObject instanceof String)

Let’s check the above code in an example program

1
2
3
def blogname = '''coderolls''';
println (blogname instanceof String) // true
println (blogname instanceof ArrayList) // false

Output:

1
2
true
false

See the above code as GitHub Gist

In the above program, I have defined one String blogname. I have used the instanceof keyword to check if the blogname implements the String class and it prints true. It means the blogname is an instance of the String class.

When I used the instanceof keyword to check if the blogname implements the ArrayList class and it prints false. It means the blogname is not an instance of the ArrayList class.

Conclusion

In this tutorial, we have seen how we can determine the datatype of an object in Groovy.

You can use the getClass() method to determine the class of an object.

1
anObject.getClass()

Also, if you want to check if an object implements an Interface or Class, you can use the instanceof keyword.

1
(anObject instanceof String)

That’s it about checking the datatype of an object in Groovy.

If you know any other way to check the datatype of an object, please comment below to help the community. Thank you.

You can check my YouTube channel here

Join Newsletter
Get the latest tutorials right in your inbox. We never spam!

comments powered by Disqus