So, you want to learn few scripting and dynamically typed language(s) for the Java platform? The following will get a Java programmer started with Groovy and (J)Ruby. It is said that you can write Fortran in any language! While you still have to learn language specific idioms, naming, coding conventions/styles etc., the table below would help you with "quick start". To reiterate: you need to pick up language specific idioms as Reg Braithwaite notes in his comment below.
I wrote simple script samples and tried the same with command line
wrappers of jrunscript. If
you CVS checked out script engines from
scripting.dev.java.net, these scripts are in
bin directories of respective script
engine directories. I used Groovy 1.0 JSR 06 and JRuby 0.9.0 versions respectively.
| Feature | Java | Groovy | JRuby |
|---|---|---|---|
| Type System | Static with few dynamic checks inserted as needed. | Dynamic, optional static types | Dynamic | Comments |
|
|
|
| Control Statements |
* break can be used in
loops to break out of loop. * continue statement skips the current iteration of a for, while, or do-while loop and valuates the boolean expression that controls the loop. |
* do.. while
is not supported. * for (init; condition; increment) is not supported. * null is treated as false. Truth value special cases for data structures. * Unlike Java, switch statement works for arbitrary expressions (not just for integral expressions and enums as in Java). isCase method is used on objects. * Labeled break and continue are not supported (yet?) |
* nil is treated as false. C/C++ Trap: 0 is true!
* break terminates loop immediately. * redo immediately repeats w/o rerunning the condition. * next starts the next iteration through the loop. * retry restarts the loop, rerunning the condition. |
| String Literals | "hello world" |
|
|
| Class Declaration |
|
|
|
| Instance Variables |
|
Same as Java or you can omit types.
Think "def" means java.lang.Object!
John Wilson notes that 'def' is really untyped. If I declare a variable as being of type Object it is not 100% the same as 'def'ing it. Please refer to his comments below.
|
instance variable names start with '@'
|
| Static Variables |
|
Same as Java or you can omit types.
Think "def" means java.lang.Object!
John Wilson notes that 'def' is really untyped. If I declare a variable as being of type Object it is not 100% the same as 'def'ing it. Please refer to his comments below.
static def i; static def f; static def d; static def s; static k; |
Static variable names start with '@@'
|
| Global Variables | Not supported. Use public static final variables in a class or use enum as appropriate. | Top-level variables are supported. No special naming enforced. [Actually, these are contained in "Script" class - which contains all "global" definitions] |
Has to start with "$"
|
| Method Definition |
|
|
|
| Static Method Definition |
|
|
use className.methodName as method name.
|
| Returning from a method |
|
Same as Java. But can leave the return statement - in that case the last expression evaluated is returned. | Same as Java. But can leave the return statement - in that case the last expression evaluated is returned. |
| Object Creation |
|
|
'new' is a method in Ruby!
|
| Method Call |
|
can omit parans in some cases
* If no arguments, need to use parans* Parans can be omitted only for method calls in top-level statements Example:
|
can omit parans in some cases
|
| Static Method Call |
|
* If no arguments, need to use parans* Parans can be omitted only for method calls in top-level statements Example:
|
|
| Object Initialization |
|
|
|
| Referring to the current object | this | this | self |
| Null | null |
null - Guillaume Laforge notes that "Null Object Pattern" is supported in Groovy. See also: NullObject.
So, you can call null.toString() for example.
|
nil -
but "nil" is a proper object. So, methods
can be called on it without getting NullPointerException.
|
| Arrays |
|
|
|
| Array Literals |
|
Note that Groovy has List literals (represented
by a java.util.List instance). The following
list literal syntax can be used.
|
|
| RegEx Literals | Not supported. Regular Expressions are specified as Strings. |
/pattern/
Note from Guillaume Laforge: The "slashy" string syntax creates strings, in fact, it's not a regular expression per se, but it allows one to avoid to have to escape each and every character as it's the case in Java. But, this syntax doesn't really create some kind. Thanks! |
/pattern/ |
| Hash Literals | Not supported |
|
|
| varargs methods |
Use JDK 1.5+. The "..." after the last formal
argument means this is a vararg method.
|
Declare the last argument of the method as an array
with that Groovy packs "excess" arguments in method
call into that array argument
|
|
| Keyword arguments (a.k.a Named Parameters | Not supported |
Not directly supported. But hash literals can be
passed as an argument. When passing a hash literal
as an argument, the "[" and "]" chars can be omitted.
|
Not directly supported. But hash literals can be
passed as an argument. When passing a hash literal
as an argument, the "{" and "}" chars can be omitted.
|
| Operator Overloading | Not supported - except for built-in String + operator |
Special methods are defined in the class to overload
operators. For example, "plus" to overload "+".
|
Method names can be operators. Also, when calling
operator methods, you need not say obj.methodName.
instead you say obj methodName param1. But, you
can use usual method call syntax as well.
|
| Extending another class |
|
|
|
| Referring to super class method | super.foo(); | super.foo(); |
Unlike Java and Groovy, super
keyword calls the super class method of the currently executing
method. You are not calling arbitrary super method.
|
| Nested, inner and anonymous classes |
|
* Nested and inner classes are not supported (yet?). * Local and anonymous classes are not supported (yet?) But, we can use closures
|
From what I read, nested class is possible but
not "inner" classes. Please correct me if I am
wrong!
|
| Closures |
* Not yet part of Java. But, may become! * Nominal Closures for Java (version 0.2) * Full Disclosure * For now, use local and anonymous classes
|
For detailed explanation, refer to
Closures in Groovy
|
* Blocks must follow a method invocation. * Proc objects can be created by Proc.new or proc method (in Kernel).
For detailed critic, refer to
closures-in-ruby.rb
|
| Exception Handling |
|
Exception handling is same as Java - except that declarations of exceptions in method signature are optional - even for checked exceptions. |
In addition, there is another way to do non-local
jump:
The catch is searched by symbol (symbol is an
internalized string).
|
| Modules, Namespaces |
There may be
future
improvements in this area.
|
Same as Java |
require is like import.
There is also load -
but unlike "require", "load" loads the file everytime
when called (i.e., require loads and caches - subsequent
require on the same file is a no-op).
|
| Mixins | Not supported. |
Mix-in categories can be used. But, there seems to
another Mixin
proposal as well.
Guillaume Laforge notes that the @Property notation (striked above) is not needed anymore and will be forbidden in the RC-1 release. See also Groovy Beans
|
|
| Accessing Class object | obj.getClass(); | obj.class // or obj.getClass() | obj.class |
| Constructing object using reflection |
|
Same as Java - except that not forced to handle exceptions |
|
| Calling a method using reflection |
|
|
|