Scala Tutorial

Basics

Control Statements

OOP Concepts

Parameterized - Type

Exceptions

Scala Annotation

Methods

String

Scala Packages

Scala Trait

Collections

Scala Options

Miscellaneous Topics

Data Types in Scala

Scala is a statically-typed language, which means you must specify the type of a variable when you declare it, or it will be inferred by the compiler based on the initial value. Here are the most common data types in Scala:

  1. Basic Data Types:

    • Byte: 8-bit signed two's complement integer (-128 to 127).

      val b: Byte = 127
      
    • Short: 16-bit signed two's complement integer (-32,768 to 32,767).

      val s: Short = 32767
      
    • Int: 32-bit signed two's complement integer.

      val i: Int = 2147483647
      
    • Long: 64-bit signed two's complement integer.

      val l: Long = 9223372036854775807L
      
    • Float: 32-bit IEEE 754 single-precision float.

      val f: Float = 3.14f
      
    • Double: 64-bit IEEE 754 double-precision float.

      val d: Double = 3.14159265359
      
    • Char: 16-bit unsigned Unicode character (U+0000 to U+FFFF).

      val c: Char = 'A'
      
    • Boolean: true or false.

      val isTrue: Boolean = true
      
  2. String: It's not a basic type in the traditional sense, but it's used so frequently that it deserves mention. It's an immutable sequence of characters.

    val str: String = "Hello, Scala!"
    
  3. Arrays and Collections: Scala offers a variety of collection classes. The primary ones include List, Set, Map, Seq, Vector, and Array.

    • Array: Mutable, fixed-size.

      val arr: Array[Int] = Array(1, 2, 3)
      
    • List: Immutable.

      val lst: List[Int] = List(1, 2, 3)
      
    • Set: Collection of distinct elements. Can be mutable or immutable.

      val s: Set[Int] = Set(1, 2, 2, 3) // Will result in Set(1, 2, 3)
      
    • Map: Collection of key-value pairs. Can be mutable or immutable.

      val m: Map[String, Int] = Map("one" -> 1, "two" -> 2)
      
  4. Option, Some, None: These types are used to represent the presence or absence of a value.

    val opt1: Option[Int] = Some(42)
    val opt2: Option[Int] = None
    
  5. Tuples: A tuple groups together simple logical collections of items without using classes. Tuples are immutable and can hold heterogeneous data types.

    val tuple: (Int, String, Boolean) = (1, "Scala", true)
    
  6. Function Types: Functions are first-class citizens in Scala, and they have their own types.

    val func: Int => String = (x: Int) => x.toString
    

Remember that Scala is a rich language, and there are other types like case classes, objects, and various advanced collection types (like SortedSet, Queue, etc.). However, these are the fundamental building blocks for most applications.

  1. Scala primitive data types:

    • Description: Scala supports a variety of primitive data types, including integers, floating-point numbers, characters, and booleans.
    • Code Example:
      val intVariable: Int = 42
      val doubleVariable: Double = 3.14
      val charVariable: Char = 'A'
      val booleanVariable: Boolean = true
      
  2. Numeric data types in Scala:

    • Description: Scala provides various numeric data types, such as Byte, Short, Int, Long, Float, and Double.
    • Code Example:
      val intValue: Int = 42
      val floatValue: Float = 3.14f
      
  3. String and character data types in Scala:

    • Description: Strings and characters are fundamental data types in Scala. Strings are enclosed in double quotes, and characters are enclosed in single quotes.
    • Code Example:
      val str: String = "Hello, Scala!"
      val char: Char = 'A'
      
  4. Scala Boolean data type:

    • Description: The Boolean data type in Scala represents true or false values.
    • Code Example:
      val isTrue: Boolean = true
      val isFalse: Boolean = false
      
  5. Tuples and tuple data types in Scala:

    • Description: Tuples are ordered collections of elements and are used to group data. They can hold elements of different data types.
    • Code Example:
      val myTuple: (Int, String, Double) = (1, "Scala", 3.14)
      
  6. Scala collections data types:

    • Description: Scala offers a rich set of collection data types, including List, Set, Map, Array, and more, for handling groups of elements.
    • Code Example:
      val myList: List[Int] = List(1, 2, 3)
      val mySet: Set[String] = Set("apple", "banana", "orange")
      val myMap: Map[String, Int] = Map("one" -> 1, "two" -> 2)
      
  7. Option and Either data types in Scala:

    • Description: Option and Either are used for handling optional and error-prone values in a functional way.
    • Code Example:
      val maybeValue: Option[Int] = Some(42)
      val result: Either[String, Int] = Right(42)
      
  8. User-defined data types in Scala:

    • Description: Scala allows the creation of custom data types using class or case class, enabling the definition of structured and meaningful entities.
    • Code Example:
      case class Person(name: String, age: Int)
      
      val john: Person = Person("John", 30)
      
  9. Enumeration data types in Scala:

    • Description: Enums in Scala are created using the Enumeration trait, providing a set of named values.
    • Code Example:
      object Color extends Enumeration {
        val Red, Green, Blue = Value
      }
      
      val myColor: Color.Value = Color.Red
      
  10. Pattern matching with data types in Scala:

    • Description: Pattern matching is a powerful feature in Scala, allowing the extraction and matching of values against different data types.
    • Code Example:
      def matchExample(value: Any): String = value match {
        case s: String => s"String: $s"
        case i: Int => s"Integer: $i"
        case _ => "Other"
      }
      
  11. Type inference in Scala:

    • Description: Scala's type inference system automatically deduces the data type of variables and expressions, reducing the need for explicit type annotations.
    • Code Example:
      val inferredInt = 42
      val inferredString = "Scala"
      
  12. Scala Any and AnyVal data types:

    • Description: Any is the root type of the Scala type hierarchy, and AnyVal is the base type for value types (non-reference types).
    • Code Example:
      val anyValue: Any = "Any can hold any value"
      val anyValValue: AnyVal = 42