YouTip LogoYouTip

Scala Data Types

The main data types supported by Scala include basic types, collection types, and special types.\n\nThe following table lists the data types supported by Scala:\n\n| Type Category | Data Type | Description | Actual Class in Scala Standard Library |\n| --- | --- | --- | --- |\n| Basic Type | `Byte` | 8-bit signed integer, value range from -128 to 127 | `scala.Byte` |\n| Basic Type | `Short` | 16-bit signed integer, value range from -32768 to 32767 | `scala.Short` |\n| Basic Type | `Int` | 32-bit signed integer, value range from -2147483648 to 2147483647 | `scala.Int` |\n| Basic Type | `Long` | 64-bit signed integer, value range from -9223372036854775808 to 9223372036854775807 | `scala.Long` |\n| Basic Type | `Float` | 32-bit IEEE 754 single-precision floating-point number | `scala.Float` |\n| Basic Type | `Double` | 64-bit IEEE 754 double-precision floating-point number | `scala.Double` |\n| Basic Type | `Char` | 16-bit unsigned Unicode character, value range from U+0000 to U+FFFF | `scala.Char` |\n| Basic Type | `String` | String type, representing a sequence of characters | `java.lang.String` |\n| Basic Type | `Boolean` | Boolean type, values are `true` or `false` | `scala.Boolean` |\n| Collection Type | `List` | Immutable linked list | `scala.collection.immutable.List` |\n| Collection Type | `Set` | Immutable set | `scala.collection.immutable.Set` |\n| Collection Type | `Map` | Immutable key-value pair collection | `scala.collection.immutable.Map` |\n| Collection Type | `Array` | Mutable array | `scala.Array` |\n| Collection Type | `Tuple` | Immutable container that can contain elements of different types | `scala.TupleN` |\n| Collection Type | `Option` | Container representing a value that may or may not be present | `scala.Option` |\n| Collection Type | `Either` | Represents one of two possible value types | `scala.util.Either` |\n| Collection Type | `Try` | Container for handling operation results that may succeed or fail | `scala.util.Try` |\n| Special Type | `Unit` | Represents no value, equivalent to `void` in Java | `scala.Unit` |\n| Special Type | `Null` | Singleton object, representing the null value of all reference types | `scala.Null` |\n| Special Type | `Nothing` | Represents no return value type, is a subtype of all types | `scala.Nothing` |\n| Special Type | `Any` | Supertype of all types | `scala.Any` |\n| Special Type | `AnyRef` | Supertype of all reference types, equivalent to `Object` in Java | `scala.AnyRef` |\n\nIn Scala, all data types are objects.\n\nScala does not have the concept of primitive types like in Java. Although Scala's basic data types (such as Int, Double, etc.) look syntactically similar to Java's primitive types, they are actually objects. This means you can call methods on these types.\n\nFor example, the Int type in Scala is actually an instance of the scala.Int class, and scala.Int is a final class that inherits from AnyVal. AnyVal is a special class in Scala that represents value types.\n\nBelow is a complete Scala program demonstrating the use of various data types:\n\n## DataTypeExamples.scala file code:\n\nobject DataTypeExamples {\n\ndef main(args: Array): Unit ={\n\n// Basic Types\n\nval byteValue: Byte =127\n\nval shortValue: Short =32767\n\nval intValue: Int =2147483647\n\nval longValue: Long = 9223372036854775807L\n\nval floatValue: Float = 3.14f\n\nval doubleValue: Double =3.141592653589793\n\nval charValue: Char ='A'\n\nval stringValue: String ="Hello, Scala!"\n\nval booleanValue: Boolean =true\n\n// Collection Types\n\nval listValue: List= List(1, 2, 3)\n\nval setValue: Set= Set("Scala", "Java", "Python")\n\nval mapValue: Map[String, Int]= Map("one" ->1, "two" ->2, "three" ->3)\n\nval arrayValue: Array= Array(4, 5, 6)\n\nval tupleValue:(Int, String, Boolean)=(42, "Answer", true)\n\nval optionValue: Option= Some("I am here")\n\nval eitherValue: Either[String, Int]= Right(42)\n\nval tryValue: Try= Try(10 / 2)\n\n// Special Types\n\nval unitValue: Unit =()\n\nval nullValue: String =null\n\nval nothingValue: Nothing =throw new RuntimeException("Nothing value")\n\n// Output All Values\n\n println(s"Byte Value: $byteValue")\n\n println(s"Short Value: $shortValue")\n\n println(s"Int Value: $intValue")\n\n println(s"Long Value: $longValue")\n\n println(s"Float Value: $floatValue")\n\n println(s"Double Value: $doubleValue")\n\n println(s"Char Value: $charValue")\n\n println(s"String Value: $stringValue")\n\n println(s"Boolean Value: $booleanValue")\n\nprintln(s"List Value: $listValue")\n\n println(s"Set Value: $setValue")\n\n println(s"Map Value: $mapValue")\n\n println(s"Array Value: ${arrayValue.mkString(", ")}")\n\n println(s"Tuple Value: $tupleValue")\n\n println(s"Option Value: $optionValue")\n\n println(s"Either Value: $eitherValue")\n\n println(s"Try Value: $tryValue")\n\nprintln(s"Unit Value: $unitValue")\n\n println(s"Null Value: $nullValue")\n\n// nothingValue is not printed because it throws an exception\n\n}\n\n}\n\nCompile using the scalac compiler, and run using the scala command:\n\nscalac DataTypeExamples.scala scala DataTypeExamples\nOutput result:\n\nByte Value: 127Short Value: 32767Int Value: 2147483647Long Value: 9223372036854775807Float Value: 3.14Double Value: 3.141592653589793Char Value: A String Value: Hello, Scala!Boolean Value: trueList Value: List(1, 2, 3)Set Value: Set(Scala, Java, Python)Map Value: Map(one -> 1, two -> 2, three -> 3)Array Value: 4, 5, 6Tuple Value: (42,Answer,true)Option Value: Some(I am here)Either Value: Right(42)Try Value: Success(5)Unit Value: ()Null Value: null
← Scala VariablesScala Basic Syntax β†’