Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

...

Here we list all the major differences between Java and Groovy.

Math: BigDecimal arithmetics by default

Floating point number literals are BigDecimals by default. So when you type 3.14, Groovy won't create a double or a float, but will instead create a BigDecimal. This might lead people into believing that Groovy is slow for arithmetics! 

If you really want to use floats or doubles, be sure to either define such numeric variables with their float or double types, like in:

Code Block
languagegroovy
double piDouble = 3.14
float piFloat = 3.14 

Or else, you can also use suffixes like:

Code Block
languagegroovy
def piDouble = 3.14d
def piFloat = 3.14f 

See also our section on Math with Groovy.

Default imports

All these packages and classes are imported by default, i.e. you do not have to use an explicit import statement to use them:

...

  • Semicolons are optional. Use them if you like (though you must use them to put several statements on one line).
  • The return keyword is optional.
  • You can use the this keyword inside static methods (which refers to this class).
  • Methods and classes are public by default.
  • Protected in Groovy has the same meaning as protected in Java, i.e. you can have friends in the same package and derived classes can also see protected members.
  • Inner classes are not supported at the moment. In most cases you can use closures instead.
  • The throws clause in a method signature is not checked by the Groovy compiler, because there is no difference between checked and unchecked exceptions.
  • You will not get compile errors like you would in Java for using undefined members or passing arguments of the wrong type. See Runtime vs Compile time, Static vs Dynamic.

...