Scala中的类型 (Types in Scala)
Type also know as data type tells the compiler about the type of data that is used by the programmer. For example, if we initialize a value or variable as an integer the compiler will free up 4 bytes of memory space and it will hold 32 bit signed integer type.
类型也称为数据类型,它告诉编译器程序员使用的数据类型。 例如,如果我们将一个值或变量初始化为整数,则编译器将释放4个字节的内存空间,并将保留32位带符号整数类型。
在Scala中进行类型转换 (Type Casting in Scala)
Type casting is the process of converting that type from one to another. For example, We can convert int type to float type in Scala.
类型转换是将类型从一种转换为另一种的过程。 例如,我们可以在Scala中将int类型转换为float类型。
But this conversion cannot be two ways and is derived from certain types i.e. the conversion from one type to another is possible and the reverse is not. This is due to the different sizes of data type and conversion from type of size greater to a smaller one might lead to data losses.
但是这种转换不能是两种方式,并且是从某些类型派生的,即可以从一种类型转换为另一种类型,而相反则不能。 这是由于数据类型的大小不同,从大小类型转换为较小类型可能会导致数据丢失。
The following diagram shows which conversion is possible:
下图显示了可能的转换:
Know more about type hierarchy: Scala type hierarchy.
了解有关类型层次结构的更多信息: Scala类型层次结构 。
The following conversions are valid:
以下转换有效:
Character -> Integer
Short -> Integer
Byte -> Integer
Integer -> Long
Long -> Float
Float -> Double
But, the reverse conversions can be done but are invalid due to data losses. For example, Double -> Float is invalid.
但是,可以进行反向转换,但是由于数据丢失而无效。 例如, Double-> Float无效。
类型转换的类型 (Types of Type Casting)
There can be two types of typecasting as all programming languages have,
所有编程语言都有两种类型转换,
Implicit type casting
隐式转换
Explicit type casting
显式铸造
1)隐式转换 (1) Implicit type casting)
In implicit type Casting of type in Scala the compiler itself cast the type of the value/variable. This conversion can be lossy i.e. in some cases data may be lost. For example, in the case of division of two int values which can return a non-integer (float/ double) value, the result will be of integer type which can lead to data loss.
在Scala中的隐式类型转换中,编译器本身将转换值/变量的类型。 这种转换可能是有损的,即在某些情况下可能会丢失数据。 例如,如果将两个int值相除并且可以返回一个非整数(浮点/双精度)值,则结果将是整数类型,这会导致数据丢失。
Scala program to demonstrate example of implicit type casting
Scala程序演示隐式类型转换的示例
object myObject {
def main(args: Array[String]) {
val a : Int = 3421
println("a has value: " + a + " and its type is: " + a.getClass)
val c = a / 4 // result is 855.25 but will be converted to Int
println("The value of a/4: " + c + " and its type is: " + c.getClass)
}
}
Output:
输出:
a has value: 3421 and its type is: int
The value of a/4: 855 and its type is: int
In the above code, we have created a value a of type integer with value 3421, then we have divided a by 4 and stored the result in value c. This division leaves to a decimal point value but due to implicit type conversion, it is stored in integer type which two losses.
在上面的代码中,我们创建了一个值为3421的整数类型的值a ,然后将a除以4并将结果存储在值c中 。 该除法保留一个小数点值,但是由于隐式类型转换,它存储在整数类型中,该类型有两个损失。
This problem can be solved by using explicit conversion to avoid data loss but in some cases that may lead to excessive memory wastage.
通过使用显式转换来避免数据丢失,可以解决此问题,但是在某些情况下,这可能会导致过多的内存浪费。
2)显式类型转换 (2) Explicit Type Conversion)
The explicit type conversion is user-defined type conversion i.e. the user will decide that final data type of the value/variable.
显式类型转换是用户定义的类型转换,即用户将决定值/变量的最终数据类型。
The type conversion of value types can be done directly but for reference types, the conversion required asInstanceOf method.
值类型的类型转换可以直接完成,但对于引用类型,则需要asInstanceOf方法进行转换。
As the asInstanceOf method is a concrete method of Any Class, it can be used for type conversion of AnyVal object and AnyRef objects too (object conversion).
由于asInstanceOf方法是Any Class的具体方法,因此它也可以用于AnyVal对象和AnyRef对象的类型转换( 对象转换 )。
Let's see the examples of both the methods in action:
让我们来看一下这两种方法的示例:
Example 1: Explicit type conversion
示例1:显式类型转换
object myObject {
def main(args: Array[String]) {
// Type conversion from Short to Long
val a : Short = 3421
println("a has value: " + a + " and its type is: " + a.getClass)
val b : Long = a // converting type from short to long
println("Type casting from Short to Long")
println("b has value: " + b + " and its type is: " + b.getClass)
// Type conversion from Char to Float
val ch : Char = 'S'
println("\nch has value: " + ch + " and its type is: " + ch.getClass)
val fl : Float = ch // converting type from Char to Float
println("Type casting from Character to Float")
println("fl has value: " + fl + " and its type is: " + fl.getClass)
}
}
Output:
输出:
a has value: 3421 and its type is: short
Type casting from Short to Long
b has value: 3421 and its type is: longch has value: S and its type is: char
Type casting from Character to Float
fl has value: 83.0 and its type is: float
In the above code, we have done two types of conversions. One from short to long and other from char to float.
在上面的代码中,我们完成了两种类型的转换。 一个从短到长 ,另一个从char到float。
For short to long, we have created variable a of type short that stores a value 3421, and then we have created another variable b of type long which is initialized with the value of short.
对于short到long,我们创建了 short类型的变量a ,存储了一个值3421,然后创建了另一个long类型的变量b,并使用short的值对其进行了初始化。
For char to float, we have created variable ch of type char that stores a value S, and then we have created another variable fl of type float which is initialized with the value of char. This will have the float type of ASCII value of 'S'.
为了使char浮动,我们创建了char类型的变量ch ,存储了一个值S ,然后创建了另一个float类型的变量fl,该变量使用char的值进行了初始化。 这将具有ASCII值“ S”的浮点类型。
Example 2: Explicit conversion using asInstanceOf method
示例2:使用asInstanceOf方法的显式转换
object myObject {
def main(args: Array[String]) {
// Type conversion from Short to Flaot
val a : Short = 3421
println("a has value: " + a + " and its type is: " + a.getClass)
val b = a.asInstanceOf[Double] // converting type from short to long
println("Type casting from Short to Double")
println("b has value: " + b + " and its type is: " + b.getClass)
// Type conversion from Char to Int
val ch : Char = 'S'
println("\nch has value: " + ch + " and its type is: " + ch.getClass)
val intVal = ch.asInstanceOf[Int] // converting type from Char to Int
println("Type casting from Character to Int")
println("intVal has value: " + intVal + " and its type is: " + intVal.getClass)
}
}
Output:
输出:
a has value: 3421 and its type is: short
Type casting from Short to Double
b has value: 3421.0 and its type is: doublech has value: S and its type is: char
Type casting from Character to Int
intVal has value: 83 and its type is: int
In the above code, we have done two types of conversions. One from short to Double and other from char to int.
在上面的代码中,我们完成了两种类型的转换。 一个从short到Double ,另一个从char到int。
For short to Double, we have created variable a of type short that stores a value 3421, and then we have created another variable b of type Double which is initialized using the asInstanceOf method for conversion of type.
对于Double的short,我们创建了 short类型的变量a ,存储了一个值3421 ,然后创建了另一个Double类型的变量b,使用asInstanceOf方法初始化了该变量b以进行类型转换。
For char to float, we have created variable ch of type char that stores a value S, and then we have created another variable intVal of type int which is initialized with the value of char using the asInstanceOf method. The data in the intVal is the ASCII value for the character.
对于char到浮子,我们已经创建char类型的变量CH,其存储的值S,然后我们已经创建了与使用asInstanceOf方法炭的值进行初始化int类型的另一个变量INTVAL。 intVal中的数据是字符的ASCII值。
翻译自: https://www.includehelp.com/scala/type-casting.aspx