Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Substituting Groovy for Java Little By Little

Tags:

java

groovy

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:

  1. Is this a reasonable way to convert?
  2. Can I keep all of my public methods and and fields in Java? Groovy is "just" a superset, right?
  3. What kinds of things would you not do in Groovy, but prefer Java instead?
like image 435
Dan Rosenstark Avatar asked Feb 12 '10 16:02

Dan Rosenstark


2 Answers

Is this a reasonable way to convert?

Yes

Can I keep all of my public methods and and fields in Java? Groovy is "just" a superset, right?

Almost a superset, but not exactly. For example, == in Groovy calls the .equals method, 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 arrays

Object[] objs = new Object[] { 1, 2, 4 }

A 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.

What kinds of things would you not do in Groovy, but prefer Java instead?

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.

like image 139
Dónal Avatar answered Oct 12 '22 23:10

Dónal


From Wikipedia:

Most Java code is also syntactically valid Groovy.

And

Some Groovy code looks like it would in the Java language, but Groovy is not a superset of Java. However, barring a few incompatibilities, one can usually rename a .java file to a .groovy one and it will work. Groovy allows the coder to leave out some elements which are required in Java, so Groovy code can be more compact. This makes the Groovy learning curve for Java developers gradual, since they can start with Java syntax and gradually learn to add Groovy features.

In regard to the rest of your question, I think this seems a reasonable way to progress. I would try to focus on changing related classes to use Groovy together though, so that i'ts easy to know which classes use which language when assigning tasks to developers, who may not know both languages.

like image 25
Mr. Boy Avatar answered Oct 12 '22 23:10

Mr. Boy