当前位置:网站首页>Scala类型转换
Scala类型转换
2022-08-02 08:20:00 【Hyf 。】
目录
Java (隐式类型转换)
原则:精度小的类型自动转换为精度大的数值类型,这 个就是自动类型转换(隐式转换)。有它对应的类型就输出什么类型,如果没有它的类型,就会遵循从精度小的数值类型自动转换为精度大的数值类型。
package chapter01;
public class TestDataTypeConversion {
public static void main(String[] args) {
byte b = 10;
test(b);
char c = 'a';
test(c);
//精度小的类型自动转换为精度大的数值类型,这个就是自动类型转换(隐式转换)
}
public static void test(byte b){
System.out.println("bbb");
}
public static void test(short s){
System.out.println("sss");
}
public static void test(char c){
System.out.println("ccc");
}
public static void test(int i){
System.out.println("iii");
}
}代码测试

Scala数值类型自动转换
char类型只能转换成int类型
原则:当 Scala 程序在进行赋值或者运算时,精度小的类型自动转换为精度大的数值类型,这 个就是自动类型转换(隐式转换)。数据类型按精度(容量)大小排序为:

基本说明:
(1)自动提升原则:有多种类型的数据混合运算时,系统首先自动将所有数据转换成精度大的那种数据类型,然后再进行计算。
(2)把精度大的数值类型赋值给精度小的数值类型时,就会报错,反之就会进行自动类型转换。
(3)(byte,short)和char之间不会相互自动转换。
(4)byte,short,char他们三者可以计算,在计算时首先转换为int类型。
运行代码
package chapter02
object Test_08DataTypeConversion {
def main(args: Array[String]): Unit = {
//(1)自动提升原则:有多种类型的数据混合运算时,系统首先自动将所有数据转换成
// 精度大的那种数据类型,然后再进行计算。
val a1:Byte = 10
val b1:Long = 2353L
val result1 :Long=(a1+b1)
val result2 :Int =(a1+b1.toInt) //强制数值类型转换
//(2)把精度大的数值类型赋值给精度小的数值类型时,就会报错(除非强制数值类型转换),反之就会进行自动
//类型转换。
val a2:Byte =8
val b2:Int =a2
// val c2:Byte =b2 //会报错的,除非强制数值类型转换
//(3)(byte,short)和 char 之间不会相互自动转换。
val a3:Byte =9
val b3:Char ='a'
//val c3:Byte =b3 //虽然IDEA没有报错,但是它是错误的,除非强制数值类型转换
val c3:Int = b3
println(c3)
//(4)byte,short,char 他们三者可以计算,在计算时首先转换为 int 类型。
val a4:Byte =12
val b4:Short=25
val c4:Char ='c'
val result4:Int=a4+b4
val result44:Int=a4+b4+c4
println(result44)
}
}测试截图

边栏推荐
- Installation and use of pnpm
- Technology Cloud Report: To realize the metaverse, NVIDIA starts from building an infrastructure platform
- 图扑软件数字孪生油气管道站,搭建油气运输管控平台
- shell中计算命令详解(expr、(())、 $[]、let、bc )
- (Note)阿克西斯ACASIS DT-3608双盘位硬盘阵列盒RAID设置
- PyQt5 (a) PyQt5 installation and configuration, read from the folder and display images, simulation to generate the sketch image
- Biotin - LC - Hydrazide | CAS: 109276-34-8 | Biotin - LC - Hydrazide
- postman下载安装汉化及使用
- 普林斯顿微积分读本03第二章--编程实现函数图像绘制、三角学回顾
- JSP页面中page指令有哪些属性及方法可使用呢?
猜你喜欢

etcd实现大规模服务治理应用实战
MySQL ODBC驱动简介

【论文阅读】Distilling the Knowledge in a Neural Network

小康股份更名赛力斯,如何走出一条高端产品的“丝绸之路”?

How Engineers Treat Open Source --- A veteran engineer's heartfelt words

抓包工具Charles修改Response步骤

postman下载安装汉化及使用

在 QT Creator 上配置 opencv 环境的一些认识和注意点

【开源项目】X-TRACK源码分析

Write a small game in C (three chess)
随机推荐
EPSANet: An Efficient Pyramid Split Attention Block on Convolutional Neural Network
Stop mental exhaustion Daily sharing
AttributeError: module ‘clr‘ has no attribute ‘AddReference‘
十大免费cms建站系统介绍推荐
(Note)阿克西斯ACASIS DT-3608双盘位硬盘阵列盒RAID设置
The packet capture tool Charles modifies the Response step
[OC学习笔记]Block三种类型
Detailed explanation of calculation commands in shell (expr, (()), $[], let, bc )
PyCharm使用教程(较详细,图+文)
用C写小游戏(三子棋)
ip地址那点事(二)
高仿【华为消费者业务官网】和精彩动画剖析:练习在低代码平台中嵌入JS代码
编程与哲学(2)——输出是为了更好的输入
自定义table表格
The custom table form
Biotin - LC - Hydrazide | CAS: 109276-34-8 | Biotin - LC - Hydrazide
How Engineers Treat Open Source --- A veteran engineer's heartfelt words
文章解读 -- FlowNet3D:Learning Scene Flow in 3D Point Clouds
Database triggers and transactions
day_05模块