views:

451

answers:

4

I don't want to break any rules here but happy christmas everyone of you! In a silent hour an idea crossed my mind:

Is it possible to replace any java coding which I use daily with groovy or scala? E.g. writing small webapps which include servlets/portlets etc.

A: 

yes. both are compiled for the same VM, you can use Java classes in them. the programming language syntax is just sugar coating on the JVM bytecode, which is the same no matter what.

just somebody
actually the bytecode is NOT the same no matter what. Both Groovy and Scala have runtime components that incur memory and speed penalties to do their "sugar coating" magic. Both are slower sometimes by a large margin.
fuzzy lollipop
different compilers emit different code for different sources, that's no wonder. my point was that both scalac and javac target the same processor, the JVM. "the same bytecode" meant "code for the same processor".
just somebody
+7  A: 

I've completely replaced my server side processing/data crunching that would previously be written in Java with Scala. It's made life a lot easier, and a lot more fun.

  • Small servlets for REST webservices written on top of Step (web pico "framework", it's a single code file, comically small servlet wrapper) http://github.com/alandipert/step. Scala's xml handling combined with a simple json outputter (I use Twitter's) makes this completely painless.
  • Hibernate + Annotations as my persistence layer (very painless once you've cleaned up the Hibernate's collection handling/types)
  • Various data crunching background tasks.

It's certainly possible, and a really simple transition to make. Just start writing Scala as if you were writing Java, at it's worst it's just Java but much less verbose. From there you can gradually pick up the Scala concepts over time: Options, functional concepts, closures etc.

Michael
+1  A: 

I use Groovy all the time for utilities, both on the command line and on the web. Often, the utilities use jars/class files from my project, since it is all on the JVM.

For web utils, take a look at Groovlets. You can come up to speed with Groovlets in a couple of hours. A groovlet is simply a servlet distilled down to its essence.

If you need to persist state, Grails is a leading web framework (with a higher learning curve).

I don't know about portlets per se, as that is its own beast.

Michael Easter
+2  A: 

I have been using Groovy for a few months now and find that it addresses a lot of the things that have been bothering me about Java for a number of years (handling collections, null pointers, verbosity). The principal is that you should be able to take your Java source file, rename it to .groovy and start converting gradually ... that isn't quite true because Groovy doesn't support inner classes, for loops with multiple loop variables, do..while, and character literals, but these are easy to fix.

Scala is the statically-typed alternative ... Bill Venners reckons it allows you to achieve the same as Java (with compile-time checking) in about half the number of lines of code. And Scala has the LIFT framework, which is less mature than Grails but still promising.

Both Groovy and Scala are worth exploring, and will (eventually) make you more productive.

Andrew Whitehouse