O'Reilly logo

Learning Groovy by Adam L. Davis

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Appendix A: Java/Groovy 1

Feature

Java

Groovy

Public class

public class

class

Loops

for(Type it : c){...}

c.each {...}

Lists

List list = asList(1,2,3);

def list = [1,2,3]

Maps

Map m = ...; m.put(x,y);

def m = [x: y]

Function definition

void method(Type t) {}

def method(t) {}

Mutable value

Type t

def t

Immutable value

final Type t

final t

Null safety

(x == null ? null : x.y)

x?.y

Null replacement

(x == null ? "y" : x)

x ?: "y"

Sort

Collections.sort(list)

list.sort()

Wildcard import

import java.util.*;

import java.util.*

Var-args

(String... args)

(String... args)

Type parameters

Class<T>

Class<T>

Concurrency

Fork/Join

GPars

No Java Analogue

Feature

Groovy

Default closure arg

it

Default value

def method(t = "yes")

Add method to object

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required