scala数据类型
Scala中的数据类型 (Data Types in Scala)
In a programming language, a data type is also known as type, tells the compiler about the type of data that is used by the programmer i.e. if we initialize a variable as a float then the compiler will give it 8 bytes of memory space and it will hold a decimal point value.
在编程语言中,数据类型也称为类型,它告诉编译器程序员使用的数据类型,即如果我们将变量初始化为浮点型,则编译器将为其提供8个字节的内存空间,将保留小数点值。
The Scala data types are taken as it is from Java and the storage and length are the same. There are many different types of data types in Scala. We have listed commonly used data type, their syntax and working example,
Scala数据类型直接来自Java,并且存储和长度相同。 Scala中有许多不同类型的数据类型 。 我们列出了常用的数据类型,其语法和工作示例,
Scala中的数据类型类型 (Types of data types in Scala)
Boolean
布尔型
Integer
整数
Float
浮动
Double
双
Char
烧焦
String
串
Nothing
没有
Any
任何
anyRef
任何参考
anyVar
任何变量
1)布尔值 (1) Boolean)
This data type has only two values true or false.
此数据类型只有两个值true或false 。
It takes up the storage of 2 bytes.
它占用2个字节的存储空间。
The boolean data type is generally used for checking conditional statements and has a default value false.
布尔数据类型通常用于检查条件语句,其默认值为false 。
Syntax:
句法:
var bool : Boolean = false;
Example:
例:
object MyClass {
def main(args: Array[String]) {
var bool : Boolean = true;
println("The boolean value is " + bool);
if(bool){
println("IncludeHelp")
}
}
}
Output
输出量
The boolean value is true
IncludeHelp
2)整数 (2) Integer)
This data type is the most commonly used in programming.
此数据类型是编程中最常用的。
Its values range from -2147483648 to 2147483647.
其值的范围是-2147483648至2147483647。
Storage needed to be allotted is 32 bit (4 bytes).
需要分配的存储为32位(4字节)。
Commonly used for general numerical calculations and the default value is 0.
通常用于常规数值计算,默认值为0 。
Syntax:
句法:
var i : Int = 233;
Example:
例:
object MyClass {
def main(args: Array[String]) {
var i : Int = 243;
println("The number is " + i);
}
}
Output
输出量
The number is 243
3)浮动 (3) Float)
This data type is the most commonly used in programming for the decimal point numbers.
该数据类型是编程中最常用的小数点数字。
Its values are IEEE 754 single-precision float.
其值为IEEE 754单精度浮点数。
Storage needed to be allotted is 32 bit (4 bytes).
需要分配的存储为32位(4字节)。
Commonly used for general numerical calculations and the default value is 0.0F.
通常用于常规数值计算,默认值为0.0F 。
Syntax:
句法:
var i : Float = 23.3f;
Example:
例:
object MyClass {
def main(args: Array[String]) {
var i : Float = 243.623f;
println("The Floating Point is " + i);
}
}
Output
输出量
The Floating Point is 243.623
4)双 (4) Double)
This data type is used to handle decimal point numbers that need larger value and more precision.
此数据类型用于处理需要更大的值和更高的精度的小数点数字。
Its values are IEEE 754 double-precision float.
其值为IEEE 754双精度浮点数。
Storage needed to be allotted is 64 bit (8 bytes).
需要分配的存储为64位(8字节)。
The default value is 0.0D.
默认值为0.0D 。
Syntax:
句法:
var i : Double = 23.3;
OR
var i : Double = 23.3d;
Example:
例:
object MyClass {
def main(args: Array[String]) {
var doublevar : Double = 243.62343d;
println("The Double float variable's value is " + doublevar);
}
}
Output
输出量
The Double float variable's value is 243.62343
5)字符 (5) Char)
This data type is used to handle character assignments stored as unsigned Unicode characters.
此数据类型用于处理存储为无符号Unicode字符的字符分配。
Its values range from Range:0 to 216-1 Unicode.
其值的范围是0到216-1 Unicode。
Storage needed to be allotted is 16 bit (2 bytes).
需要分配的存储为16位(2字节)。
The default value is '\u000'.
默认值为'\ u000' 。
Syntax:
句法:
var ch : Char = 'I';
Example:
例:
object MyClass {
def main(args: Array[String]) {
var ch : Char = 'I';
println("The character value of the variable ch is " + ch);
}
}
Output
输出量
The character value of the variable ch is I
6)弦 (6) String)
The string data type is used to store a character sequence in a program.
字符串数据类型用于在程序中存储字符序列。
Stings are a very common type of input and a string variable can accept a string of any length.
字符串是输入的一种非常常见的类型,字符串变量可以接受任何长度的字符串。
An in-depth explanation of string class is given in string in Scala.
The default value of the string variable is Null.
字符串变量的默认值为Null 。
Syntax:
句法:
var name : String = "IncludeHelp";
OR
var name = "IncludeHelp";
Example:
例:
object MyClass {
def main(args: Array[String]) {
var name : String = "IncludeHelp";
println("Hello Welcome to " + name);
}
}
Output
输出量
Hello Welcome to IncludeHelp
7)没事 (7) Nothing)
In Scala, Nothing is treated as the subtype of all types in Scala. Based on this Nothing is a subtype of Char, Float, Int and all the other data type as mention in data types and variables in Scala article.
在Scala中, 没有任何东西被视为Scala中所有类型的子类型。 基于此, Nothing是Char , Float , Int的子类型,以及Scala文章中的数据类型和变量中提到的所有其他数据类型。
Now, where is Scala Nothing data Type used? It has found its applications when you need to return nothing and that too without an error.
现在,在哪里使用Scala Nothing数据类型? 当您不需要返回任何内容并且也没有错误时,它已经找到了它的应用程序。
Suppose, if a function returns something only if a condition is true otherwise Nothing is returned, then in the else case there can be an error. To avoid this Scala uses Nothing.
假设,如果一个函数仅在条件为true的情况下才返回某些内容,否则不返回任何内容,则在else情况下可能会出现错误。 为了避免这种情况,Scala使用Nothing 。
8)任何 (8) any)
The any class is treated as the superclass of all classes in Scala. It is at the root of the class hierarchy in Scala. This means every class that exists in Scala is by default a sub-class of Scala.
any类在Scala中被视为所有类的超类。 它是Scala中类层次结构的根源。 这意味着默认情况下,Scala中存在的每个类都是Scala的子类。
The any class has two subclasses they are: 1) anyRef, 2) anyVar
any类具有两个子类: 1)anyRef , 2)anyVar
9)任何参考 (9) anyRef)
The anyRef class is the root class of all reference type in Scala.
anyRef类是Scala中所有引用类型的根类。
10)anyVar (10) anyVar)
The anyVar class is the root class of all value classes in Scala.
anyVar类是Scala中所有值类的根类。
All data classes are valued class and are a subclass of the anyVar class.
所有数据类都是有值类,是anyVar类的子类。
All the classes are categorized as follows :
所有类的分类如下:
Char , Byte , Short are subrange types.
Char,Byte,Short是子范围类型。
Int , Long are integer types.
Int和Long是整数类型。
Float , Double are floating point types.
Float,Double是浮点类型。
翻译自: https://www.includehelp.com/scala/data-types-in-scala.aspx
scala数据类型