Search⌘ K

Groovy for Scala Devs

Discover how Groovy compares to Scala and Java with a focus on syntax differences and handling of null values. Explore setup guidance for Java and Groovy to enhance your understanding and workflow.

Java vs. Scala

Feature

Java

Scala

Public Class

public class

class

Loops

for(Type it : c){…}

c.foreach {…}

Lists

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

val list = List(1,2,3)

Maps

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

val m = Map(x -> y)

Function Def.

void method(Type t) {}

def method(t: Type) = {}

Mutable Value

Type t

var t: Type

Immutable Value

final Type t

val t: Type

Null safety

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

for (a <- Option(x)) yield a.y

Null replacement

(x == null ? “y” : x)

Option(x) getOrElse “y”

Sort

Collections.sort(list)

list.sort(_ < _)

Wildcard import

import java.util.*;

import scala.collection._

Var-args

(String… args) 

(args: String*)

Type parameters

Class<T>

Class[T]

Concurrency

Fork/Join

Akka

...