Immutable variables (val) and value types

Introduction to Scala

David Venturi

Curriculum Manager, DataCamp

The game of Twenty-One

Twenty-One card values

1 http://bit.ly/twenty_one_wikipedia
Introduction to Scala

Scala has two kinds of variables

val (immutable)

  • can't be reassigned

Four of hearts

scala> val fourHearts: Int = 4
fourHearts: Int = 4

var (mutable)

Introduction to Scala

Reassigning a val produces an error

scala> val fourHearts: Int = 4
fourHearts: Int = 4
scala> fourHearts = 5
<console>:12: error: reassignment to val
       fourHearts = 5
                  ^
Introduction to Scala

Scala value types

  • Double
  • Float
  • Long
  • Int
  • Short
  • Byte
  • Char
  • Boolean
  • Unit
Introduction to Scala

Scala value types

Most common types for data-related tasks:

  • Double
  • Int
  • Boolean
  • String
Introduction to Scala

Double

  • 64-bit IEEE-754 double-precision floating point number
  • 4.94065645841246544e-324d to 1.79769313486231570e+308d (positive or negative)
Introduction to Scala

Double

Pi symbol

scala> val piDouble: Double = 3.14
piDouble: Double = 3.14
Introduction to Scala

Double

Pi symbol

scala> val piFloat: Float = 3.14
<console>:11: error: type mismatch;
 found   : Double(3.14)
 required: Float
       val piFloat: Float = 3.14
Introduction to Scala

Double is more precise than Float

Pi symbol

scala> val piDouble: Double = 3.14159265358979323846264338327
piDouble: Double = 3.141592653589793
scala> val piFloat: Float = 3.14159265358979323846264338327f
piFloat: Float = 3.1415927
Introduction to Scala

Int

  • 32-bit signed integer
  • -2^31 to 2^31-1, inclusive
  • -2,147,483,648 to 2,147,483,647
Introduction to Scala

Int

Four of hearts

scala> val fourHearts: Int = 4
fourHearts: Int = 4
Introduction to Scala

Boolean

  • true or false

Hand value of 24 with a 7, 8, and 9

scala> val handBusts: Boolean = true
handBusts: Boolean = true
Introduction to Scala

Char and String

Char

  • 16-bit unsigned Unicode integer
  • 0 to 2^16-1, inclusive
  • 0 to 65,535

String

  • String: a sequence of Char

scala> val symbolAceSpades: String = "A?"

symbolAceSpades: String = A?

Introduction to Scala

Scala value types

  • Double
  • Float
  • Long
  • Int
  • Short
  • Byte
  • Char
  • Boolean
  • Unit
Introduction to Scala

Scala value types

  • scala.Double
  • scala.Float
  • scala.Long
  • scala.Int
  • scala.Short
  • scala.Byte
  • scala.Char
  • scala.Boolean
  • scala.Unit
Introduction to Scala

Scala value types

scala> val fourHearts: Int = 4
fourHearts: Int = 4
scala> val fiveHearts: scala.Int = 5
fiveHearts: Int = 5
Introduction to Scala

Scala value types have equivalent Java types

Scala types
  • scala.Double
  • scala.Float
  • scala.Long
  • scala.Int
  • scala.Short
  • scala.Byte
  • scala.Char
  • scala.Boolean
  • scala.Unit
Java types
  • java.lang.Double
  • java.lang.Float
  • java.lang.Long
  • java.lang.Integer
  • java.lang.Short
  • java.lang.Byte
  • java.lang.Character
  • java.lang.Boolean
Introduction to Scala

Choosing the right type

  • Double
  • Float
  • Long
  • Int
  • Short
  • Byte
  • Char
  • Boolean
  • Unit
Introduction to Scala

Let's practice!

Introduction to Scala

Preparing Video For Download...