Jython v.s. Groovy
August 31, 2008 § 4 Comments
A commenter reminds me on Groovy (It was mentioned as part of Grails). His comment makes me curious on what’s the latest on Groovy and how it is stacking up to Jython.
Groovy’s differences vs Python is subtle. In fact, Groovy is way more similar to Ruby.
Couple of Groovy Syntax that looks like Ruby:
- Groovy: d.collect { k, v -> k+1 }
- Ruby: d.collect {|k,v| k + 1 }
- Groovy: rng = 1..3
- Ruby: rng = 1..3
- Groovy: (1..3).each { i -> println i }
- Ruby: [1..3].each { |x| print x }
- Groovy: def function_name(x) { return x; }
- Ruby: def function_name(x); return x end
Both Groovy and Jython do very good job in minimizing JAVA’s complexity. Groovy is more familiar to JAVA programmer than Jython but not by much since its syntax is Ruby-ish.
Now, next question is: Can I use existing JAVA packages from inside Groovy or Jython?
- Groovy import statement is similar to JAVA. That’s how it can access other JAVA packages.
- Jython access other JAVA packages via Python import statement (e.g. from java.util import Random) while also having access to Python standard packages (e.g. sys or os).
Comparing Groovy vs Jython is like comparing Ruby vs Python. Both are easy to read and comfortable to programmers.
Using either one is a time-saver when developing in JAVA. Definitely use either one if you are dynamic language kind-of-guy and forced to develop in JAVA.
Quick Facts on Groovy:
- It’s a dynamic language written on top of JVM (JAVA is a compile to byte-code language, just like CPython).
- Its syntax is indeed very similar to Python and Ruby.
- It has GroovyConsole.
- It was created around 2003-2004.
- It was created to overcome JAVA’s overly verbose syntax.
Quick Facts on Jython:
- It’s Python implemented on top of JVM.
- Jython is compiled to JAVA byte-code level.
- It has Python Interpreter Console.
- Jython release number is consistent with CPython release number (The latest stable is 2.2).
- Because the latest stable is 2.2, Jython does not support Python’s decorator.
- It was created in 1997.
- It was created to overcome JAVA’s overly verbose syntax.
- Download
- Run this command: java -jar jython_installer-2.2.1.jar
- Groovy Documentation
- Groovy Beginner Tutorial
- How to connect to database from Jython? ‘from com.ziclix.python.sql import zxJDBC’
Couple mistakes in your Ruby…
A literal range is 1..3, not [1..3]. The latter is a range as the first element of a literal array.
Ruby also can do single-line methods, as in
def function_name; end
@Charles Nutter:
Updated post per your correction
this
(1..3).each { i -> println “${i}” }
could be this:
(1..3).each { i -> println i }
or this
(1..3).each { println it }
@ dbachelder:
Updated post per your suggestions