Skip to content

Latest commit

 

History

History
279 lines (247 loc) · 5.36 KB

java-scala-diff.md

File metadata and controls

279 lines (247 loc) · 5.36 KB

Some differences between Java and Scala styles

Wrapping long lines

// in Java, indent 4 spaces
stringBuilder
    .append("one ")
    .append("two ")
    .append("three");
doSomnething(
    param1,
    param2, 
    param3);
// in Scala, indent 2 spaces
stringBuilder
  .append("one ")
  .append("two ")
  .append("three")
doSomnething(
  param1,
  param2, 
  param3)

// but 4 spaces for constructor parameters
class Person(
    name: String,
    birthDate: Date,
    shoeSize: Double)
  extends Entity {
  def firstMethod: Foo = ...
}
// in Java, inflix operator on next line
int x = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9
    + 10 + 11 + 12 + 13 + 14 + 15 + 16
    + 17 + 18 + 19 + 20;
// in Scala, inflix method on previous line, otherwise compiler might infer semicolon unexpectedly
val x = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
  10 + 11 + 12 + 13 + 14 + 15 + 16 + 17 +
  18 + 19 + 20

Wrapping long parameter lists

// in Scala, each parameter on separate line, indented from first line (not parenthesis)
foo(
  someVeryLongFieldName,
  andAnotherVeryLongFieldName,
  "this is a string",
  3.1415926)

Upper/lower case

// in Java, classes, interfaces, annotations are in UpperCamelCase
@MyAnnotation
class MyClass implements MyInterface { ... }
// in Scala, classes and traits are in UpperCamelCase, but annotations are in lowerCamelCase
@myAnnotation
class MyClass extends MyTrait { ... }

// objects are usually in UpperCamelCase but can be in lowerCamelCase if they mimic a package or a function
object ast {
  sealed trait Expr

  case class Plus(e1: Expr, e2: Expr) extends Expr { ... }
}

object inc {
 def apply(x: Int): Int = x + 1
}
// in Java, constants are in UPPER_CASE
class Foo {
  static final int MY_CONSTANT = 239;
}
// in Scala, constants are in UpperCamelCase
object Foo {
  val MyConstant = 239
}

Getters, setters

// in Java, “get”/“set” prefix
class Foo {
  int getBar() {...}

  void setBar(int bar) {...}
}
// in Scala, just the name of a property
class Foo {
  def bar: Int = ...

  def bar_=(bar: Int) { ... }
}

// in rare cases when it’s necessary to back getter/setter by a field, underscore prefix to disambiguate
class Foo {
  private var _bar = -1
}

Methods without parameters

// in Scala, no parentheses if a method acts like a getter and has no side effects
def nickname = firstName

Type parameters

// in Java, usually named T or another single upper-case letter
void <T, K, V> doSomething(List<T> list, Map<K, V> map) { ... }
// in Scala, usually first letters of alphabet or words in UpperCamelCase
def doSomething[A, Key, Value](list: List[A], map: Map[Key, Value]): Unit = ...

Curly braces

// in Java, surrounding spaces are optional
a = new int[] {5, 6};
b = new int[] { 5, 6 };
// in Scala, separate by spaces
tuples.filter { case (s1, s2) => s1 == s2 }
// in Java, opening brace may be on next line
class Foo
{
  ...
}
// in Scala, opening brace on same line, otherwise compiler might infer semicolon unexpectedly
class Foo {
  ...
}
// in Java, always use with if/else/for/do/while
if (foo) {
  doSomething();
}
else {
  doSomethingElse();
}
// in Scala, omit if a pure-functional operation and all branches are single-line
val x = if (foo)
  doSomething()
else
  doSomethingElse()

// don’t omit if no “else”
if (foo) {
  doSomething()
}

// always omit in “case”
val x = foo match {
  case 1 =>
    doSomething()
    doSomethingMore()
  case 2 => doSomethingElse()
}

// omit if “yield”
for (i <- 1 to 5) yield i

// don’t omit if no “yield”
for (i <- 1 to 5) {
  println(i)
}

// omit in one-line methods
def add(a: Int, b: Int): Int = a + b

// OK to put it on the next line
def sum(ls: List[String]): Int =
  ls.map(_.toInt).foldLeft(0)(_ + _)

// omit in methods consisting of a single “match”
def sum(ls: List[Int]): Int = ls match {
  case hd :: tail => hd + sum(tail)
  case Nil => 0
}

Ternary operator

// in Java
x = foo ? bar : baz;
// in Scala
x = if (foo) bar else baz

Class contents order

In Java, “some logical order”. In Scala, fields before methods.

Annotations

// in Java, may be on the same line
@Override public int hashCode() {
  ...
}

@Partial @Mock DataLoader loader;
// in Scala, each annotation on its own line
@transaction
@throws(classOf[IOException])
override protected final def foo(): Unit = {
  ...
}

Modifier order

// in Java
public protected private abstract default static final transient volatile synchronized native strictfp
// in Scala
override protected private implicit final lazy

Javadoc, Scaladoc

// Javadoc

/**
 * Does something.
 *
 * Some more details.
 */
void doSomething() {
  ...
}
// Scaladoc, asterisks in third column by IntelliJ IDEA’s default

/** Does something.
  *
  * Some more details.
  */
def doSomething(): Unit = {
  ...
}