O'Reilly logo

Learning Jupyter by Dan Toomey

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

Scala immutability

Immutable means you cannot change something. In Scala, all variables are immutable unless specifically marked otherwise. This is the opposite of languages such as Java, where all variables are mutable unless specifically marked otherwise.

In Java, we can have the following function:

public void calculate(integer amount) { 
} 

We can modify the value of amount inside the calculate function. We can tell Java not to allow changing the value if we use the final keyword:

public void calculate(final integer amount) { 
} 

Whereas in Scala, the similar routine is as follows:

def calculate (amount: Int): Int = { 
  amount = amount + 1;
  return amount;
}

The preceding code leaves the value of the amount variable as it was before the routine was ...

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