当前位置:网站首页>变量和数据类型(03)
变量和数据类型(03)
2022-07-24 06:18:00 【Hyf 。】
目录
整数类型(Byte、Short、Int、Long)
Scala 的整数类型就是用于存放整数值的,比如 12,30,3456 等等。
整型分类
| 数据类型 | 描述 |
| Byte[1] | 8位有符号补码整数。数值区间为-128到127 |
| Short[2] | 16位有符号补码整数。数值区间为-32768到32767 |
| Int[4] | 32位有符号补码整数。数值区间为-2147483648到2147483647 |
| Long[8] | 64 位有符号补码整数。数值区间为 -9223372036854775808 到 9223372036854775807 = 2 的(64-1)次方-1 |
案例操作
Scala 的整型常量默认为 Int型
运行代码
package chapter02
object Test07_DataType {
def main(args: Array[String]): Unit = {
//整数类型
val a1: Byte = 127
val a2: Byte = -128
// val a2:Byte= 128 //超出Byte范围会报错
val a3 = 18 //默认为int类型
val a4:Long = 1263561245174L //长整型数值定义(l或者L都可以)
//编译器能够自动识别为Byte类型
val b1:Byte = 10
val b2:Byte = (10+20)
println(b2)
//val b3:Byte = b1 + 20 这种学法不正确,真报错
val b3:Byte =(b1+20).toByte //强制类型转换(可以输出)
println(b3)
}
}
运行结果

浮点类型
Scala 的浮点类型可以表示一个小数,比如 123.4f,7.8,0.12 等等
浮点型分类
| 数据类型 | 描述 |
| Float[4] | 32 位, IEEE 754 标准的单精度浮点数 |
| Double[8] | 64 位 IEEE 754 标准的双精度浮点 |
案例实操
Scala 的浮点型常量默认为 Double 型,声明 Float 型常量,须后加‘f’或‘F’。
运行代码
package chapter02
object Test07_DataType {
def main(args: Array[String]): Unit = {
//浮点类型
val f1:Float =2.3132f //(Float类型)
val f2 = 2.27672 //(Double类型)
}
}
字符类型(Char)
基本说明
字符类可以表示单个字符,字符类型是Char。
案例实操
(1)字符常量是用单引号 ' ' 括起来的单个字符。
(2)\t :一个制表位,实现对齐的功能。
(3)\n : 换行符。
(4)\\ : 表示\
(5)\" : 表示 "
案例操作
运行代码
package chapter02
object Test07_DataType {
def main(args: Array[String]): Unit = {
//字符类型
val c1:Char = 'a'
println(c1)
val c2:Char = '9'
println(c2)
val c3:Char ='\t' //制表符
val c4:Char ='\n' //换行符
println("ljj"+ c3 + "hyf")
println("ljj"+ c4 + "hyf")
//转义符
val c5 = '\\' //表示 \ 本身
val c6 = '\"' //表示 ”
println("ljj"+ c5 + "hyf")
println("ljj"+ c6 + "hyf")
}
}
运行结果

字符变量底层保存ASCII码
案例操作

布尔类型:Boolean
基本说明
(1)布尔类型也叫Boolean类型,Boolean类型数据只允许取值true和false
(2)Boolean类型占1个字节。
案例实操
package chapter02
object Test07_DataType {
def main(args: Array[String]): Unit = {
//布尔类型
val isTrue:Boolean = true
println(isTrue)
}
}
运行结果

边栏推荐
- 先爱自己,再爱别人。
- Requirements already satisfied: and read timed out. problem solving methods appear during the installation of snownlp package
- Tensorflow Einstein function
- 你是谁由你自己决定!
- 【杂论:离散化】
- UE4/5 无法打开文件“xxx.generated.h”(Cannot open file xxx.generated.h)的解决方法总结
- [USB voltmeter and ammeter] Based on stm32f103c8t6 for Arduino
- 安装snownlp包过程出现Requirement already satisfied:及Read timed out.问题解决方法
- 反射
- MGR_ mysqlsh_ Keepalive high availability architecture deployment document
猜你喜欢
随机推荐
Redis master-slave mechanism
Cmake notes
Redis 分片集群
Redis 主从机制
17. 什么情况用ArrayList or LinkedList呢?
Don't care too much about others' eyes, it will erase your glory
UE4/5 无法打开文件“xxx.generated.h”(Cannot open file xxx.generated.h)的解决方法总结
安装snownlp包过程出现Requirement already satisfied:及Read timed out.问题解决方法
别太在意别人的眼光,那会抹杀你的光彩
Create WPF project
Don't care too much about what others think of you
Prompt for garbled code when CHM file is opened
【方向盘】超爱的IDEA提效神器Save Actions,卸载了
Three level classification / menu query tree structure
MySQL gets the self incrementing line mark (different from MySQL version)
[learning notes] what happens when the URL is input into the page presentation?
js和ts学习总结
xavier_normal_ 初始化测试
《大厂面试》之JVM篇21问与答
XXL execute node error log swiping









