Like many programming languages, Scala provides a set of built-in data types to represent various values. Here are some of the most common built-in data types in Scala:
Int: Represents 32-bit signed integers. Example: val myInt: Int = 42
Long: Represents 64-bit signed integers. Example: val myLong: Long = 1234567890123L
Float: Represents single-precision floating-point numbers. Example: val myFloat: Float = 3.14f
Double: Represents double-precision floating-point numbers. Example: val myDouble: Double = 3.14159265359
Char: Represents a single Unicode character. Example: val myChar: Char = 'A'
Boolean: Represents a Boolean value (true or false). Example: val isTrue: Boolean = true
String: Represents a sequence of characters. Example: val myString: String = "Hello, Scala"
Byte: Represents 8-bit signed integers. Example: val myByte: Byte = 127
Short: Represents 16-bit signed integers. Example: val myShort: Short = 32767
BigInt: Represents arbitrarily large integers. Example: val bigIntValue: BigInt = BigInt("1234567890123456789012345678901234567890")
BigDecimal: Represents arbitrarily precise decimal numbers. Example: val bigDecimalValue: BigDecimal = BigDecimal("3.14159265358979323846264338327950288419716939937510")
Unit: Represents the absence of a value, similar to void in other languages. It's often used as the return type for functions that perform side effects. Example: def printHello(): Unit = { println("Hello, Scala") }
Null: Represents a null reference. This type is not typically used explicitly; it's more relevant when dealing with interoperability with Java.
Any: The supertype of all types in Scala's type system. This means any value can have the type Any. Example: val anyValue: Any = 42
AnyRef: The supertype of all reference types (similar to Java's Object). Example: val anyRefValue: AnyRef = "Hello"
Nothing: A subtype of all types, often used to indicate that a function doesn't return normally (e.g., throws an exception). It's also used to represent empty collections. Example: def throwError(): Nothing = throw new Exception("An error occurred")
Option: Represents optional values used to avoid null references. It has two subclasses: Some (with a value) and None (no value). Example: val maybeValue: Option[Int] = Some(42)
These built-in data types provide the foundation for working with various kinds of data in Scala. Additionally, Scala allows you to define your custom data types using classes and case classes, enabling you to create more complex and meaningful data structures for your applications.
I'm sharing more.