I have been checking out Groovy a bit and I feel that moving a Java program to Groovy little by little — grabbing a class and making it a Groovy class, then converting the method guts a bit at a time — might be a relatively sane way to take advantage of some of the Groovy language features. I would also do new classes in Groovy.
Questions:
- Is this a reasonable way to convert?
- Can I keep all of my public methods and and fields in Java? Groovy is “just” a superset, right?
- What kinds of things would you not do in Groovy, but prefer Java instead?
Yes
Almost a superset, but not exactly. For example,
==in Groovy calls the.equalsmethod, but in Java it checks whether 2 references refer to the same object. Another example is that Groovy does not support this syntax for constructing arraysA final example is that when an overloaded method is called, Groovy uses the runtime type of the parameters when choosing which method to invoke, whereas Java uses the compile-time parameter types. This page has a fairly comprehensive list of differences between the two languages.
I write everything in Groovy, because I’m much more productive with Groovy than Java, and I enjoy Groovy programming a lot more. I would only use Java if there was a performance problem with some code AND I could demonstrate that writing it in Java resolved the problem. In practice, this has never actually happened to me.
There may also be socio-political reasons to use Java, e.g. some code needs to be maintained by many people some of which don’t know Groovy and don’t want to learn it.