© Toby Weston 2018
Toby WestonScala for Java Developershttps://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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.