|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: "Groovy: How To Determine The Datatype Of An Object?" |
| 4 | +author: gaurav |
| 5 | +categories: [Groovy] |
| 6 | +description: "In this quick tutorial, we will how can we determine the datatype of an object in Groovy." |
| 7 | +--- |
| 8 | + |
| 9 | +## Introduction |
| 10 | + |
| 11 | +In this quick tutorial, we will see how we can determine the datatype of an object in Groovy. |
| 12 | + |
| 13 | +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. |
| 14 | + |
| 15 | +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. |
| 16 | + |
| 17 | +## How To Determine The Datatype Of An Object In Groovy? |
| 18 | + |
| 19 | +To determine the class of an object, you can simply call the `getClass()` method on it. |
| 20 | + |
| 21 | +An example is given below. |
| 22 | +```groovy |
| 23 | +anObject.getClass() |
| 24 | +``` |
| 25 | +let check the above line program. |
| 26 | + |
| 27 | +```groovy |
| 28 | +def names = ['Gaurav', 'Shubham', 'Nayan', 'Sudeep']; |
| 29 | +
|
| 30 | +println names.getClass(); // prints 'class java.util.ArrayList' |
| 31 | +
|
| 32 | +def blogname = '''coderolls'''; |
| 33 | +
|
| 34 | +println blogname.getClass(); // prints 'class java.lang.String' |
| 35 | +
|
| 36 | +println blogname.getClass().name; // prints 'java.lang.String' |
| 37 | +``` |
| 38 | + |
| 39 | +Output: |
| 40 | +``` |
| 41 | +class java.util.ArrayList |
| 42 | +class java.lang.String |
| 43 | +java.lang.String |
| 44 | +``` |
| 45 | +In the above program, we can see that the `names` is an `ArrayList` and `blogname` is a `String` object. |
| 46 | + |
| 47 | +When we call the `getClass()` method on these objects it prints its respective datatype. |
| 48 | + |
| 49 | +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`. |
| 50 | + |
| 51 | +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. |
| 52 | + |
| 53 | +For example, |
| 54 | + |
| 55 | +```groovy |
| 56 | +(anObject instanceof String) |
| 57 | +``` |
| 58 | + |
| 59 | +Let's check above code in an example program |
| 60 | + |
| 61 | +```groovy |
| 62 | +def blogname = '''coderolls'''; |
| 63 | +println (blogname instanceof String) // true |
| 64 | +println (blogname instanceof ArrayList) // false |
| 65 | +``` |
| 66 | +Output: |
| 67 | +``` |
| 68 | +true |
| 69 | +false |
| 70 | +``` |
| 71 | + |
| 72 | +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. |
| 73 | + |
| 74 | +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. |
| 75 | + |
| 76 | +## Conclusion |
| 77 | + |
| 78 | +In this tutorial, we have seen how we can determine the datatype of an object in Groovy. |
| 79 | + |
| 80 | +You can use the `getClass()` method to determine the class of an object. |
| 81 | + |
| 82 | +```groovy |
| 83 | +anObject.getClass() |
| 84 | +``` |
| 85 | + |
| 86 | +Also, if you want to check if an object implements an Interface or Class, you can use the `instanceof` keyword. |
| 87 | + |
| 88 | +```groovy |
| 89 | +(anObject instanceof String) |
| 90 | +``` |
| 91 | +That's it about checking the datatype of an object in Groovy. |
| 92 | + |
| 93 | +If you know any other way to check the datatype of an object, please comment below to help the community. Thank you. |
| 94 | + |
| 95 | +### Related Articles |
| 96 | + |
| 97 | +- [Hello World In Groovy](https://coderolls.com/hello-world-in-groovy/) |
0 commit comments