O'Reilly logo

Programming Groovy 2 by Venkat Subramaniam

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Why Dynamic Languages?

Dynamic languages have the ability to extend a program at runtime, including changing types, behaviors, and object structures. With these languages, we can do things at runtime that static languages do at compile time; we can even execute program statements that are created on the fly at runtime.

For example, if we want to compute a five percent raise on an $80,000 salary, we could simply write the following:

 
5.percentRaise(80000)

Yes, that’s the friendly java.lang.Integer responding to our own dynamic method, which we can add quite easily, like so:

 
Integer​.metaClass.percentRaise = { amount -> amount * (1 + delegate / 100.0) }

As we see here, it’s easy to add dynamic methods to classes in Groovy. The dynamic method ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required