Defining Scala variables and constants

One of the big things that’s different in Scala is that defining variables is simpler. Just the name will do, Scala will work out what type of variable by what you put in it. Here are 3 examples. The first is a constant, the second a variable and in the third I specify the type:

object Runner extends App {
  
  // string constant 
  val name = "Vincent"
  
  // actual variable - not a constant
  var limit = 21
  
  // defining a value specifying type
  val year:Long = 2016
  
}

The ‘val’ keyword indicates a constant and ‘var’ a variable. When writing in a more functional style code should avoid having variables and only have constants.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s