Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groovy as List Question

Tags:

groovy

I ran across this bit of code and to me it seems like its not needed is there any reason to do the following

def answers = [] as List

instead of

def answers = []

In groovy I thought that [] was the empty list so there would be no need to have the as List

like image 707
Jeff Beck Avatar asked May 11 '11 13:05

Jeff Beck


People also ask

How do I create a list in Groovy?

A List literal is presented as a series of objects separated by commas and enclosed in square brackets. To process the data in a list, we must be able to access individual elements. Groovy Lists are indexed using the indexing operator []. List indices start at zero, which refers to the first element.

What is Groovy-> operator?

-> is a symbol indicating the end of parameters list for a closure in Groovy.

How do I know the type of a variable in Groovy?

You can use the getClass() method to determine the class of an object. Also, if you want to check if an object implements an Interface or Class, you can use the instanceof keyword. That's it about checking the datatype of an object in Groovy.


1 Answers

No, there is no difference, both create an ArrayList and List (java.util.List) is an interface anyway.

like image 50
mfloryan Avatar answered Dec 07 '22 11:12

mfloryan