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