📜  Scala 中的数据类型

📅  最后修改于: 2022-05-13 01:55:26.936000             🧑  作者: Mango

Scala 中的数据类型

数据类型是数据的分类,它告诉编译器变量具有哪种类型的值。例如,如果变量具有 int 数据类型,则它保存数值。在 Scala 中,数据类型在长度和存储方面与Java相似。在 Scala 中,数据类型被视为相同的对象,因此数据类型的第一个字母是大写字母。
Scala 中可用的数据类型如下表所示:

DataTypeDefault valueDescription
BooleanFalseTrue or False
Byte08 bit signed value. Range:-128 to 127
Short016 bit signed value. Range:-215 to 215-1
Char‘\u000’16 bit unsigned unicode character. Range:0 to 216-1
Int032 bit signed value. Range:-231 to 231-1
Long0L64 bit signed value. Range:-263 to 263-1
Float0.0F32 bit IEEE 754 single-Precision float
Double0.0D64 bit IEEE 754 double-Precision float
StringnullA sequence of characterUnitCoinsides to no value.NothingIt is a subtype of every other type and it contains no value.AnyIt is a supertype of all other typesAnyValIt serve as value types.AnyRefIt serves as reference types.

注意: Scala 不像Java中那样包含原始类型的概念。

例如:

// Scala program to illustrate Datatypes
object Test
{
def main(args: Array[String]) 
{
    var a: Boolean = true
    var a1: Byte = 126
    var a2: Float = 2.45673f
    var a3: Int = 3
    var a4: Short = 45
    var a5: Double = 2.93846523
    var a6: Char = 'A'
    if (a == true) 
    {
    println("boolean:geeksforgeeks")
    }
    println("byte:" + a1)
    println("float:" + a2)
    println("integer:" + a3)
    println("short:" + a4)
    println("double:" + a5)
    println("char:" + a6)
}
}

输出:

boolean:geeksforgeeks
byte:126
float:2.45673
integer:3
short:45
double:2.93846523
char:A

字面量中的文字:这里我们将讨论 Scala 中使用的不同类型的字面量。

  • Integral 字面量:这些通常是 int 类型或 long 类型(使用“L”或“I”后缀)。一些合法的整数字面量是:
  • Floating-point Literals: These are of float type(“f” or”F” suffix used ) and of double type.

    0.7
    1e60f
    3.12154f
    1.0e100
    .3

  • Boolean Literals: These are of Boolean type and it contains only true and false.
  • Symbol Literals: In Scala, symbol is a case class. In symbol literal, a’Y’ is identical to scala.Symbol(“Y”).

    package scala
    final case class Symbol private (name: String) {
    override def toString: String = “‘” + name
    }

  • Character Literals: In Scala, character literal is a single character that is encircled between single quotes.There characters are printable unicode character and also described by the escape character. Few valid literals are shown below:

    ‘\b’
    ‘a’
    ‘\r’
    ‘\u0027’

  • String Literals: In Scala, string literals are the sequence of character that are enclosed between double quotes. Some valid literals as shown below:

    “welcome to \n geeksforgeeks”
    “\\This is the tutorial of Scala\\”

  • Null Values: In Scala, null value is of scale.Null type, that’s the way it is adaptable with every reference type. It is indicated as a reference value which refers to a special “null” object.
  • Multi-line Literals: In Scala, multi-line literals are the sequence of characters that are encircled in between triple quotes. In this new line and other control characters are valid. Some valid multi-line literals shown below:

    “””welcome to geeksforgeeks\n
    this is the tutorial of \n
    scala programing language”””