Scala铸造

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:

下图显示了可能的转换:

Scala | Type Casting

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,

所有编程语言都有两种类型转换,

  1. Implicit type casting

    隐式转换

  2. 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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/540548.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

OpenCV探索之路(二十五):制作简易的图像标注小工具

搞图像深度学习的童鞋一定碰过图像数据标注的东西,当我们训练网络时需要训练集数据,但在网上又没有找到自己想要的数据集,这时候就考虑自己制作自己的数据集了,这时就需要对图像进行标注。图像标注是件很枯燥又很费人力物力的一件…

图论 弦_混乱的弦

图论 弦Problem statement: 问题陈述: You are provided an input string S and the string "includehelp". You need to figure out all possible subsequences "includehelp" in the string S? Find out the number of ways in which the s…

「原创」从马云、马化腾、李彦宏的对话,看出三人智慧差在哪里?

在今年中国IT领袖峰会上,马云、马化腾、李彦宏第一次单独合影,同框画面可以说很难得了。BAT关心的走势一直是同行们竞相捕捉的热点,所以三位大Boss在这次大会上关于人工智能的见解,也受到广泛关注与多方解读。马云认为机器比人聪明…

字符串矩阵转换成长字符串_字符串矩阵

字符串矩阵转换成长字符串Description: 描述: In this article, we are going to see how backtracking can be used to solve following problems? 在本文中,我们将看到如何使用回溯来解决以下问题? Problem statement: 问题陈述&#xf…

java awt 按钮响应_Java AWT按钮

java awt 按钮响应The Button class is used to implement a GUI push button. It has a label and generates an event, whenever it is clicked. As mentioned in previous sections, it extends the Component class and implements the Accessible interface. Button类用于…

qgis在地图上画导航线_在Laravel中的航线

qgis在地图上画导航线For further process we need to know something about it, 为了进一步处理,我们需要了解一些有关它的信息, The route is a core part in Laravel because it maps the controller for sending a request which is automatically …

Logistic回归和SVM的异同

这个问题在最近面试的时候被问了几次,让谈一下Logistic回归(以下简称LR)和SVM的异同。由于之前没有对比分析过,而且不知道从哪个角度去分析,一时语塞,只能不知为不知。 现在对这二者做一个对比分析&#xf…

构建安全网络 比格云全系云产品30天内5折购

一年之计在于春,每年的三、四月,都是个人创业最佳的起步阶段,也是企业采购最火热的时期。为了降低用户的上云成本,让大家能无门槛享受到优质高性能的云服务,比格云从3月16日起,将上线“充值30天内&#xff…

数据结构 基础知识

一。逻辑结构: 是指数据对象中数据 素之间的相互关系。 其实这也是我 今后最需要关注的问题 逻辑结构分为以 四种1. 集合结构 2.线性结构 3.数形结构 4,图形结构 二。物理结构: 1,顺序存储结,2 2. 链式存储结构 一,时间复杂…

ruby 变量类中范围_Ruby中的类

ruby 变量类中范围Ruby类 (Ruby Classes) In the actual world, we have many objects which belong to the same category. For instance, I am working on my laptop and this laptop is one of those laptops which exist around the globe. So, this laptop is an object o…

以云计算的名义 驻云科技牵手阿里云

本文讲的是以云计算的名义 驻云科技牵手阿里云一次三个公司的牵手 可能会改变无数企业的命运 2017年4月17日,对于很多人来说可能只是个平常的工作日,但是对于国内无数的企业来说却可能是个会改变企业命运的日。驻云科技联合国内云服务提供商阿里云及国外…

浏览器端已支持 ES6 规范(包括 export import)

当然,是几个比较优秀的浏览器,既然是优秀的浏览器,大家肯定知道是那几款啦,我就不列举了,我用的是 chrome。 对 script 声明 type 为 module 后就可以享受 es6 规范所带来的模块快感了。 基础语法既然是全支持&#xf…

一文读懂深度学习框架下的目标检测(附数据集)

从简单的图像分类到3D位置估算,在机器视觉领域里从来都不乏有趣的问题。其中我们最感兴趣的问题之一就是目标检测。 如同其他的机器视觉问题一样,目标检测目前为止还没有公认最好的解决方法。在了解目标检测之前,让我们先快速地了解一下这个领…

设计一个应用程序,以在C#中的按钮单击事件上在MessageBox中显示TextBox中的文本...

Here, we took two controls on windows form that are TextBox and Button, named txtInput and btnShow respectively. We have to write C# code to display TextBox’s text in the MessageBox on Button Click. 在这里,我们在Windows窗体上使用了两个控件&…

Oracle优化器:星型转换(Star Query Transformation )

Oracle优化器:星型转换(Star Query Transformation )Star query是一个事实表(fact table)和一些维度表(dimension)的join。每个维度表都跟事实表通过主外键join,且每个维度表之间不j…

JavaScript | 声明数组并使用数组索引分配元素的代码

Declare an array, assign elements by indexes and print all elements in JavaScript. 声明一个数组&#xff0c;通过索引分配元素&#xff0c;并打印JavaScript中的所有元素。 Code: 码&#xff1a; <html><head><script>var fruits [];fruits[0]"…

Kubernetes基础组件概述

本文讲的是Kubernetes基础组件概述【编者的话】最近总有同学问Kubernetes中的各个组件的相关问题&#xff0c;其实这些概念内容在官方文档中都有&#xff0c;奈何我们有些同学可能英文不好&#xff0c;又或者懒得去看&#xff0c;又或者没有找到&#xff0c;今天有时间就专门写…

c语言将链表写入二进制文件_通过逐级遍历将二进制树转换为单链表的C程序

c语言将链表写入二进制文件Problem statement: Write a C program to convert a binary tree into a single linked list by traversing level-wise. 问题陈述&#xff1a;编写一个C程序&#xff0c;通过逐级遍历将二进制树转换为单个链表 。 Example: 例&#xff1a; The ab…

洛谷 P2689 东南西北【模拟/搜索】

题目描述 给出起点和终点的坐标及接下来T个时刻的风向(东南西北)&#xff0c;每次可以选择顺风偏移1个单位或者停在原地。求到达终点的最少时间。 如果无法偏移至终点&#xff0c;输出“-1”。 输入输出格式 输入格式&#xff1a; 第一行两个正整数x1,y1&#xff0c;表示小明所…

单链表遍历_单链表及其遍历实现的基本操作

单链表遍历单链表 (Single linked list) Single linked list contains a number of nodes where each node has a data field and a pointer to next node. The link of the last node is to NULL, indicates end of list. 单个链表包含许多节点&#xff0c;其中每个节点都有一…