© Toby Weston 2018

Toby Weston, Scala for Java Developers, https://doi.org/10.1007/978-1-4842-3108-1_3

3. Some Basic Syntax

Toby Weston

(1)London, UK

Defining Values and Variables

Let’s look at some syntax . We’ll start by creating a variable:

  val language: String = "Scala"

We’ve defined a variable as a String and assigned to it the value of “Scala.” I say “variable,” but we’ve actually created an immutable value rather than a variable. The val keyword creates a constant, and language cannot be modified from this point on. Immutability is a key theme you’ll see again and again in Scala.

If we will want to modify language later, we can use var instead of val. We can then reassign it if we need to.

  var language: String = "Java"  language = "Scala" ...

Get Scala for Java Developers: A Practical Primer now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.