Skip to content
Skip to breadcrumbs
Skip to header menu
Skip to action menu
Skip to quick search
Quick Search
Browse
Pages
Blog
Labels
Attachments
Mail
Advanced
What’s New
Space Directory
Feed Builder
Keyboard Shortcuts
Confluence Gadgets
Log In
Sign Up
Dashboard
Groovy
Copy Page
You are not logged in. Any changes you make will be marked as
anonymous
. You may want to
Log In
if you already have an account. You can also
Sign Up
for a new account.
This page is being edited by
.
Paragraph
Paragraph
Heading 1
Heading 2
Heading 3
Heading 4
Heading 5
Heading 6
Preformatted
Quote
Bold
Italic
Underline
More colours
Strikethrough
Subscript
Superscript
Monospace
Clear Formatting
Bullet list
Numbered list
Outdent
Indent
Align left
Align center
Align right
Link
Table
Insert
Insert Content
Image
Link
Attachment
Symbol
Emoticon
Wiki Markup
Horizontal rule
tinymce.confluence.insert_menu.macro_desc
Info
JIRA Issue
Status
Gallery
Tasklist
Table of Contents
Other Macros
Page Layout
No Layout
Two column (simple)
Two column (simple, left sidebar)
Two column (simple, right sidebar)
Three column (simple)
Two column
Two column (left sidebar)
Two column (right sidebar)
Three column
Three column (left and right sidebars)
Undo
Redo
Find/Replace
Keyboard Shortcuts Help
<p>Groovy tries to be as natural as possible for Java developers. We've tried to follow the principle of least surprise when designing Groovy, particularly for developers learning Groovy who've come from a Java background.</p><p>Here we list all the major differences between Java and Groovy.</p><h2>Default imports</h2><p>All these packages and classes are imported by default, i.e. you do not have to use an explicit <code>import</code> statement to use them:</p><ul><li>java.io.*</li><li>java.lang.*</li><li>java.math.BigDecimal</li><li>java.math.BigInteger</li><li>java.net.*</li><li>java.util.*</li><li>groovy.lang.*</li><li>groovy.util.*</li></ul><h2>Common gotchas</h2><p>Here we list the common things you might trip over if you're a Java developer starting to use Groovy.</p><ul><li>== means equals on all types. In Java there's a wierd part of the syntax where == means equality for primitive types and == means identity for objects. Since we're using autoboxing this would be very confusing for Java developers (since x == 5 would be mostly false if x was 5 <img class="emoticon emoticon-smile" data-emoticon-name="smile" border="0" src="/s/en_GB/3278/15/_/images/icons/emoticons/smile.png" alt="(smile)" title="(smile)" /> . So for simplicity == means equals() in Groovy. If you really need the identity, you can use the method "is" like foo.is(bar). This does not work on null, but you can still use == here: foo==null.</li><li>in is a keyword. So don't use it as a variable name.</li><li><p>When declaring array you can't write</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>int[] a = {1,2,3}; </pre></td></tr></table><p>you need to write</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>int[] a = [1,2,3] </pre></td></tr></table></li><li><p>If you are used to writing a for loop that looks like</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>for (int i=0; i < len; i++) {...} </pre></td></tr></table><p>in groovy you can use that too, but you can use only one count variable. Alternatives to this are</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>for (i in 0..len-1) {...} </pre></td></tr></table><p>or</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>for (i in 0..<len) {...} </pre></td></tr></table><p>or</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>len.times {...} </pre></td></tr></table></li></ul><h2>Things to be aware of</h2><ul><li>Semicolons are optional. Use them if you like (though you must use them to put several statements on one line).</li><li>The <code>return</code> keyword is optional.</li><li>You can use the <code>this</code> keyword inside static methods (which refers to this class).</li><li>Methods and classes are public by default.</li><li><code>Protected</code> 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.</li><li>Inner (non-static nested) classes are not supported. In most cases you can use <a class="confluence-link" href="/display/GROOVY/Closures+-+Informal+Guide" data-linked-resource-id="2729" data-linked-resource-type="page" data-linked-resource-default-alias="Closures - Informal Guide" data-base-url="http://docs.codehaus.org">closures</a> instead, e.g. to <a href="http://docs.codehaus.org/display/GROOVY/Groovy+way+to+implement+interfaces">implement interfaces</a>. Static nested classes are supported.</li><li>The <code>throws</code> clause in a method signature is not checked by the Groovy compiler, because there is no difference between checked and unchecked exceptions.</li><li>You will not get compile errors like you would in Java for using undefined members or passing arguments of the wrong type. See <a class="confluence-link" href="/display/GROOVY/Runtime+vs+Compile+time%2C+Static+vs+Dynamic" data-linked-resource-id="9240616" data-linked-resource-type="page" data-linked-resource-default-alias="Runtime vs Compile time, Static vs Dynamic" data-base-url="http://docs.codehaus.org">Runtime vs Compile time, Static vs Dynamic</a>.</li></ul><h2>Uncommon Gotchas</h2><p>Java programmers are used to semicolons terminating statements and not having <a class="confluence-link" href="/display/GROOVY/Closures+-+Informal+Guide" data-linked-resource-id="2729" data-linked-resource-type="page" data-linked-resource-default-alias="Closures - Informal Guide" data-base-url="http://docs.codehaus.org">closures</a>. Also there are instance initializers in class definitions. So you might see something like:</p><table class="wysiwyg-macro" data-macro-name="code" data-macro-default-parameter="Java" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6SmF2YX0&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>class Trial { private final Thing thing = new Thing ( ) ; { thing.doSomething ( ) ; } } </pre></td></tr></table><p>Many Groovy programmers eschew the use of semicolons as distracting and redundant (though others use them all the time - it's a matter of coding style). A situation that leads to difficulties is writing the above in Groovy as:</p><table class="wysiwyg-macro" data-macro-name="code" data-macro-default-parameter="Java" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6SmF2YX0&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>class Trial { private final thing = new Thing ( ) { thing.doSomething ( ) } } </pre></td></tr></table><p>This will throw a <code>MissingMethodException</code>!</p><p>The issue here is that in this situation the newline is not a statement terminator so the following block is treated as a <a class="confluence-link" href="/display/GROOVY/Closures+-+Informal+Guide" data-linked-resource-id="2729" data-linked-resource-type="page" data-linked-resource-default-alias="Closures - Informal Guide" data-base-url="http://docs.codehaus.org">closure</a>, passed as an argument to the <code>Thing</code> constructor. Bizarre to many, but true. If you want to use instance initializers in this sort of way, it is effectively mandatory to have a semicolon:</p><table class="wysiwyg-macro" data-macro-name="code" data-macro-default-parameter="Java" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6SmF2YX0&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>class Trial { private final thing = new Thing ( ) ; { thing.doSomething ( ) } } </pre></td></tr></table><p>This way the block following the initialized definition is clearly an instance initializer.</p><p>Another document lists some <a class="confluence-link" href="/display/GROOVY/Things+you+can+do+but+better+leave+undone" data-linked-resource-id="46956559" data-linked-resource-type="page" data-linked-resource-default-alias="Things you can do but better leave undone" data-base-url="http://docs.codehaus.org">pitfalls</a> you should be aware of and give some advice on best practices to avoid those pitfalls. </p><h2>New features added to Groovy not available in Java</h2><ul><li><a class="confluence-link" href="/display/GROOVY/Closures" data-linked-resource-id="11403308" data-linked-resource-type="page" data-linked-resource-default-alias="Closures" data-base-url="http://docs.codehaus.org">Closures</a></li><li>native <a class="confluence-link" href="/display/GROOVY/Collections" data-linked-resource-id="2732" data-linked-resource-type="page" data-linked-resource-default-alias="Collections" data-base-url="http://docs.codehaus.org">syntax</a> for lists and maps</li><li><a class="confluence-link" href="/display/GROOVY/GroovyMarkup" data-linked-resource-id="2779" data-linked-resource-type="page" data-linked-resource-default-alias="GroovyMarkup" data-base-url="http://docs.codehaus.org">GroovyMarkup</a> and GPath support</li><li>native support for <a class="confluence-link" href="/display/GROOVY/Regular+Expressions" data-linked-resource-id="2768" data-linked-resource-type="page" data-linked-resource-default-alias="Regular Expressions" data-base-url="http://docs.codehaus.org">regular expressions</a></li><li>polymorphic <a class="confluence-link" href="/display/GROOVY/Looping" data-linked-resource-id="2758" data-linked-resource-type="page" data-linked-resource-default-alias="Looping" data-base-url="http://docs.codehaus.org">iteration</a> and powerful <a class="confluence-link" href="/display/GROOVY/Logical+Branching" data-linked-resource-id="2721" data-linked-resource-type="page" data-linked-resource-default-alias="Logical Branching" data-base-url="http://docs.codehaus.org">switch statement</a></li><li>dynamic and static typing is supported - so you can omit the type declarations on methods, fields and variables</li><li>you can embed expressions inside <a class="confluence-link" href="/display/GROOVY/Strings+and+GString" data-linked-resource-id="2771" data-linked-resource-type="page" data-linked-resource-default-alias="Strings and GString" data-base-url="http://docs.codehaus.org">strings</a></li><li>lots of new helper methods added to the <a href="http://groovy.codehaus.org/groovy-jdk.html">JDK</a></li><li>simpler syntax for writing <a class="confluence-link" href="/display/GROOVY/Groovy+Beans" data-linked-resource-id="2716" data-linked-resource-type="page" data-linked-resource-default-alias="Groovy Beans" data-base-url="http://docs.codehaus.org">beans</a> for both properties and adding event listeners</li><li><a class="confluence-link" href="/display/GROOVY/Statements#Statements-Statements-Safenavigation" data-anchor="Statements-Safenavigation" data-linked-resource-id="2769" data-linked-resource-type="page" data-linked-resource-default-alias="Statements#Statements-Safenavigation" data-base-url="http://docs.codehaus.org">safe navigation</a> using the ?. operator, e.g. "variable?.field" and "variable?.method()" - no more nested ifs to check for null clogging up your code</li></ul>
Please type the word appearing in the picture.
Attachments
Labels
Location
Watch this page
< Edit
Preview >
Loading…
Save
Cancel
Next hint
search
attachments
weblink
advanced