当前位置:网站首页>变量和数据类型(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)
}
}
运行结果

边栏推荐
- Redis basic type - combined with set
- Getting started with redis
- UE4/5 无法打开文件“xxx.generated.h”(Cannot open file xxx.generated.h)的解决方法总结
- tensorflow scatter_ Nd function
- Vs2019 configuration running open3d example
- (note sorting is not completed) [graph theory: find the shortest path of single source]
- 聚合型新生态模式-分享购,会员及奖励制度
- 渗透学习-SQL注入篇-靶场篇-安全狗的安装与绕过实验(后续还会更新)
- 你就是你,没有人可以取代
- Neo4j modify tag name
猜你喜欢

PyTorch 深度学习实践 第10讲/作业(Basic CNN)

第二部分—C语言提高篇_2. 内存分区

Penetration learning - SQL injection - shooting range - installation and bypass experiment of safety dog (it will be updated later)

Redis fragment cluster

owasp top10 渗透测试

【学习笔记】Web页面渲染的流程

Metaltc5.0 realizes webrtc version IPC of Junzheng pure C

【方向盘】IDEA的代码审查能力,来保证代码质量

Sealos packages and deploys kubesphere container platform

【学习笔记】网页出现白屏可能的原因与优化方法
随机推荐
Redis fragment cluster
10分钟就能写出来的——25~30K的国外企业招聘面试考题,这不是轻轻松松吗~
HashSet to array
Redis special data type bitmap
Tensorflow Einstein function
Vs debugging
Camera Hal OEM module ---- CMR_ grab.c
js和ts学习总结
SPI——发送16位和8位数据
不要太在意别人对你的看法
Job search memo
Upload excel file
GE口:SGMII模式和serdes模式
Lambda expressions sort list objects in multiple fields
Gangster escape 3
你是谁由你自己决定!
17. 什么情况用ArrayList or LinkedList呢?
MySQL gets the self incrementing line mark (different from MySQL version)
Upload pictures Base64
Day (0~6) represents the starting position of the first day of each month, stop represents the number of days of each month, and there are two blank spaces between each day. Input different days and s