日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Scala铸造

發(fā)布時間:2025/3/11 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Scala铸造 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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.

類型也稱為數(shù)據(jù)類型,它告訴編譯器程序員使用的數(shù)據(jù)類型。 例如,如果我們將一個值或變量初始化為整數(shù),則編譯器將釋放4個字節(jié)的內存空間,并將保留32位帶符號整數(shù)類型。

在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.?

但是這種轉換不能是兩種方式,并且是從某些類型派生的,即可以從一種類型轉換為另一種類型,而相反則不能。 這是由于數(shù)據(jù)類型的大小不同,從大小類型轉換為較小類型可能會導致數(shù)據(jù)丟失。

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.

但是,可以進行反向轉換,但是由于數(shù)據(jù)丟失而無效。 例如, 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中的隱式類型轉換中,編譯器本身將轉換值/變量的類型。 這種轉換可能是有損的,即在某些情況下可能會丟失數(shù)據(jù)。 例如,如果將兩個int值相除并且可以返回一個非整數(shù)(浮點/雙精度)值,則結果將是整數(shù)類型,這會導致數(shù)據(jù)丟失。

    Scala program to demonstrate example of implicit type casting

    Scala程序演示隱式類型轉換的示例

    object myObject {def main(args: Array[String]) {val a : Int = 3421println("a has value: " + a + " and its type is: " + a.getClass)val c = a / 4 // result is 855.25 but will be converted to Intprintln("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.

    在上面的代碼中,我們創(chuàng)建了一個值為3421的整數(shù)類型的值a 然后將a除以4并將結果存儲在值c中 。 該除法保留一個小數(shù)點值,但是由于隱式類型轉換,它存儲在整數(shù)類型中,該類型有兩個損失。

    This problem can be solved by using explicit conversion to avoid data loss but in some cases that may lead to excessive memory wastage.

    通過使用顯式轉換來避免數(shù)據(jù)丟失,可以解決此問題,但是在某些情況下,這可能會導致過多的內存浪費。

    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.

    顯式類型轉換是用戶定義的類型轉換,即用戶將決定值/變量的最終數(shù)據(jù)類型。

    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 = 3421println("a has value: " + a + " and its type is: " + a.getClass)val b : Long = a // converting type from short to longprintln("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 Floatprintln("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,我們創(chuàng)建了 short類型的變量a ,存儲了一個值3421,然后創(chuàng)建了另一個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浮動,我們創(chuàng)建了char類型的變量ch ,存儲了一個值S ,然后創(chuàng)建了另一個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 = 3421println("a has value: " + a + " and its type is: " + a.getClass)val b = a.asInstanceOf[Double] // converting type from short to longprintln("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 Intprintln("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,我們創(chuàng)建了 short類型的變量a ,存儲了一個值3421 ,然后創(chuàng)建了另一個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到浮子,我們已經創(chuàng)建char類型的變量CH,其存儲的值S,然后我們已經創(chuàng)建了與使用asInstanceOf方法炭的值進行初始化int類型的另一個變量INTVAL。 intVal中的數(shù)據(jù)是字符的ASCII值。

    翻譯自: https://www.includehelp.com/scala/type-casting.aspx

    總結

    以上是生活随笔為你收集整理的Scala铸造的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。

    主站蜘蛛池模板: 成人永久免费视频 | 日韩一区二区精品视频 | 免费在线观看成年人视频 | 精品少妇| www.一区二区 | 亚洲香蕉| 中文字幕制服诱惑 | 日韩高清影视 | 国产精品人人妻人人爽人人牛 | 四虎影像 | 日韩黄页网站 | 欧美一区二区三区免费看 | 国产乱妇4p交换乱免费视频 | 午夜福利视频 | 理论片国产 | 黄色成年人 | 久久午夜夜伦鲁鲁片无码免费 | 国产视频一区二区三区在线 | 激情天堂网 | 欧美mv日韩mv国产网站 | 欧美日韩一区二区不卡 | 五月婷婷丁香花 | 亚洲欧洲精品在线 | 成年人在线观看视频 | 超碰96在线| 长腿校花无力呻吟娇喘的视频 | 天天操天天操天天操天天 | a毛片毛片av永久免费 | 日韩手机在线观看 | 玖色视频 | 久久久久亚洲AV | 欧美专区视频 | 国产又粗又猛又爽又黄91精品 | 日韩视频一区二区三区在线播放免费观看 | 国内毛片毛片 | 中文字幕在线播放视频 | 天天射一射 | 日韩福利在线 | 韩国无码一区二区三区精品 | 成人乱人乱一区二区三区一级视频 | 男女无遮挡做爰猛烈视频 | 波多野结衣视频免费观看 | 日本黄页网站免费大全 | 成人av入口| 国产无码精品久久久 | 国产欧美日韩免费 | 二级黄色片 | 山村大伦淫第1部分阅读小说 | 色奇米| 欧美成人免费在线观看视频 | 亚洲成a人片 | 91抖音在线观看 | 误杀1电影免费观看高清完整版 | eeuss鲁丝片一区二区三区 | www.国产91 | 国产区在线观看视频 | 成人福利片 | 久久视频免费观看 | 一级黄色片毛片 | 亚洲成人一区二区三区 | 久久久久99精品成人片直播 | 一本一道久久综合狠狠老精东影业 | 激情五月av | caoporn国产 | 亚洲国产午夜 | 涩涩视频免费看 | 日日夜夜91 | 极品美女扒开粉嫩小泬 | jizz视频在线观看 | 亚洲超丰满肉感bbw 悠悠av | 亚洲男女 | 欧美三级三级三级爽爽爽 | 亚洲精品乱码久久久久久久 | 韩国成人在线 | 在线一区二区三区视频 | 欧美成人a∨高清免费观看 国产精品999视频 | 亚洲乱码国产乱码精品精98午夜 | 天天操夜夜草 | 亚洲va在线 | 久久久久久久精 | 日韩欧美一区在线 | 超碰在线成人 | 国产女人视频 | wwwav网站 | 日韩69 | 久久成人免费电影 | 日韩在线观看免费高清 | 在线免费观看一区二区 | 国产伦精品一区二区三区 | 农村妇女精品一区二区 | 亚洲成人二区 | 亚洲不卡视频在线 | 99热伊人| 在线视频福利 | 亚洲一级无毛 | 色图综合 | 国产成人激情 | 国产精品久久777777换脸 | 久久精品a |