当前位置:网站首页>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)
}
}测试截图

边栏推荐
- PyCharm usage tutorial (more detailed, picture + text)
- 【电子电路】长按键拉低电平,适用在有休眠机制的MCU但是没有看门狗,一个按键多个功能场景下使用
- BGP通过MPLS解决路由黑洞
- Detailed explanation of calculation commands in shell (expr, (()), $[], let, bc )
- 知识点滴 - 为什么一般不用铜锅做菜
- day_05模块
- MySQL ODBC驱动简介
- USACO美国信息学奥赛竞赛12月份开赛,中国学生备赛指南
- R language plotly visualization: plotly visualizes the scatter plot of the actual value of the regression model and the predicted value of the regression, analyzes the prediction performance of the re
- JSP页面中page指令contentPage/pageEncoding具有什么功能呢?
猜你喜欢
随机推荐
Postman download localization of installation and use
高仿【华为消费者业务官网】和精彩动画剖析:练习在低代码平台中嵌入JS代码
知识点滴 - 为什么一般不用铜锅做菜
In a recent build figure SLAM, and locate the progress
RestTemlate源码分析及工具类设计
prometheus监控mysql_galera集群
cas:139504-50-0 美登素DM1|Mertansine|
Ansible learning summary (11) - detailed explanation of forks and serial parameters of task parallel execution
Biotin-LC-Hydrazide|CAS:109276-34-8|生物素-LC-酰肼
tf中tensor的大小输出
构建Flink第一个应用程序
十大免费cms建站系统介绍推荐
postman使用方法
What attributes and methods are available for page directives in JSP pages?
day——05 迭代器,生成器
A little bit of knowledge - why do not usually cook with copper pots
Biotin-EDA|CAS:111790-37-5| 乙二胺生物素
JSP页面中page指令contentPage/pageEncoding具有什么功能呢?
Flink 程序剖析
[ansible] playbook explains the execution steps in combination with the project







![Three types of [OC learning notes] Block](/img/40/edf59e6e68891ea7c9ab0481fe7bfc.png)

