Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Groovy and generics

 
Fedor Belov
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello
Why does this code compile and execute correctly?
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Groovy is a dynamically typed language and hence your variables need not be assigned the type information as these are determined dynamically at run time. That is the reason why the code executes without issues
 
Fedor Belov
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does it mean that generics are useless?

This code executes the same
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Groovy wasnt designed to behave like Java, it was designed to create a language which in dynamically typed, easy to program. Even in Java the Generics information is not part of the code at runtime, you might want to read about Type Erasure. So when it comes to Groovy it doesnt even consider the type information you specify during the generic declaration. Its there to help you use Java API. You can read more about it here.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic