Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork1
Add Groovy blogpost - Determine-datatype-of-an-object#24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
97 changes: 97 additions & 0 deletions_posts/groovy/2021-05-11-determine-a-datatype-in-groovy.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| --- | ||
| layout: post | ||
| title: "Groovy: How To Determine The Datatype Of An Object?" | ||
| author: gaurav | ||
| categories: [Groovy] | ||
| description: "In this quick tutorial, we will how can we determine the datatype of an object in Groovy." | ||
| --- | ||
| ## Introduction | ||
| In this quick tutorial, we will see how we can determine the datatype of an object in Groovy. | ||
| 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. | ||
| ```groovy | ||
| anObject.getClass() | ||
| ``` | ||
| let check the above line program. | ||
| ```groovy | ||
| 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: | ||
| ``` | ||
| class java.util.ArrayList | ||
| class java.lang.String | ||
| java.lang.String | ||
| ``` | ||
| 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, | ||
| ```groovy | ||
| (anObject instanceof String) | ||
| ``` | ||
| Let's check above code in an example program | ||
| ```groovy | ||
| def blogname = '''coderolls'''; | ||
| println (blogname instanceof String) // true | ||
| println (blogname instanceof ArrayList) // false | ||
| ``` | ||
| Output: | ||
| ``` | ||
| true | ||
| false | ||
| ``` | ||
| 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. | ||
| ```groovy | ||
| anObject.getClass() | ||
| ``` | ||
| Also, if you want to check if an object implements an Interface or Class, you can use the `instanceof` keyword. | ||
| ```groovy | ||
| (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. | ||
| ### Related Articles | ||
| - [Hello World In Groovy](https://coderolls.com/hello-world-in-groovy/) |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.