Variable Declarations
Scala allows you to decide whether a variable is immutable (read-only) or not (readwrite) when you declare it.
immutable “variable”
an immutable “variable” is declared with the keyword val
(think value
object):
val array: Array[String] = new Array(5)
A val
must be initialized when it is declared.
mutable variable
Similarly, a mutable variable is declared with the keyword var
and it must also be initialized immediately, even though it can be changed later.
var stockPrice: Double = 100.0
Note
There are a few exceptions to the rule that you must initialize vals and vars when they are declared. For example, keyword(val
or var
) can be used with a constructor parameter.
code example
class Person(val name: String, var age: Int)
Why use immutable “variable”
Use immutable values whenever possible to eliminate a class of bugs caused by mutability.
- For example, a mutable object is dangerous as a key in hash-based maps.
- More common is unexpected behavior when an object you are using is being changed by someone else.
Ref
《Programming Scala》
Variable Declarations相关推荐
- PB修改源码时报错:C0176: Badly ordered TYPE and VARIABLE declarations. Is this modified exported source?
当出现继承关系,比如 祖先:w_master 子孙:w_child 继承自w_master 当w_child写好后(已经存在了) 在w_master中就可以写上instance变量: w_child ...
- Error: TS Error: Subsequent variable declarations must have the same type. Variable ‘beforeEach‘ mus
Error: TS Error: Subsequent variable declarations must have the same type. Variable 'beforeEach' mus ...
- PEP 526 – Syntax for Variable Annotations
PEP 526 – Syntax for Variable Annotations PEP 526 – 变量注解语法 PEP: 526 Title: Syntax for Variable Annot ...
- [Core Java® for the Impatient]重载Java2
2019独角兽企业重金招聘Python工程师标准>>> Chapter 2. Object-Oriented Programming Set(Mutator Methods)方法改变 ...
- vue css 应用变量_如何使用CSS Grid和CSS变量快速为应用创建原型
vue css 应用变量 CSS Grid and CSS Variables are both huge wins for front-end developers. The former make ...
- FPGA之道(35)Verilog中的并行与串行语句
文章目录 前言 Verilog的并行语句 Verilog连续赋值语句 普通连续赋值语句 条件连续赋值语句 Verilog程序块语句 沿事件 纯组合always 纯时序always 具有同步复位的alw ...
- 通过DBA_SOURCE定位SQL语句属于哪个存储过程
通过DBA_SOURCE定位SQL语句属于哪个存储过程 最近集中进行了一系列SQL优化,主要通过ADDM和AWR对捕捉COST或物理读.逻辑读很高的语句,问题的主要原因是全表扫描.但提交开发后开发反馈 ...
- golang 开发常见坑
目录 初级 开大括号不能放在单独的一行 未使用的变量 未使用的Imports 简式的变量声明仅可以在函数内部使用 使用简式声明重复声明变量 不能使用短变量声明来设置字段值 Can't Use Shor ...
- 代码格式化工具 uncrustify 配置文件选项详解
Uncrustify是一个代码美化工具可用于C,C++,C#,D和Java源代码的美化.它能够利用空格,tab与空格或只有tab进行缩排,添加或删除换行,排列代码等.Uncrustify具有高配置性, ...
- Linux内核网络数据包发送(三)——IP协议层分析
Linux内核网络数据包发送(三)--IP协议层分析 1. 前言 2. `ip_send_skb` 3. `ip_local_out` and `__ip_local_out` 3.1 netfilt ...
最新文章
- 比特币和加密货币入门
- 关于Cpp中的类型转换操作符
- 数据用户学术分享搜索平台——中期报告
- 实验四+040+薛龚
- bat ping 返回值_清清楚楚玩游戏!10.10版本,赛娜被动层数可以ping给队友看了
- pillow 初级用法
- Linux expect脚本使用详细说明及示例
- Java编程中“为了性能”尽量要做的26点
- Android技术栈总结
- [基础]同步消息和异步消息传递的区别?
- SPOJ Problem 2178:He is offside!
- 基于Matlab的数字图像gui界面设计
- 计算机考试后进先出,事业单位考试计算机基础知识:建立单链表——后进先出表...
- DataX-MySQL(读写)
- linux防火墙黑白名单,iptables详解(9):iptables的黑白名单机制
- Jhipster生成前端为Vue的项目引入Element-UI
- java分流什么意思_Flink如何分流数据
- sql空值用什么表示_表面粗糙度Ra为什么用0.8,1.6,3.2,6.3,12.5表示?
- 20 多个国外优秀Android开源 App ,值得收藏和学习
- Bean named '' is expected to be of type '' but was actually of type 'com.sun.proxy.$Proxy30' 异常解决
热门文章
- python只保留大写字母_python 将文件中除保留字外的小写字母换成大写字母(转换后可运行)...
- 麦吉尔大学的数学与计算机科学,麦吉尔大学数学与计算机科学本科.pdf
- 学习记录贴4:CMAQ两个脚本同时运行时冲突
- 2018 Google IO
- win10去掉快捷方式小箭头_强迫症狂喜!一招去除Win10桌面图标小箭头
- IBM Spectrum LSF-手册
- 自定义滚动条、tbody加滚动条
- LaTeX各种算法排版
- Back Projection
- 长春公办计算机学校有哪些条件,长春小学初中哪些学校民办?哪些公办?最新一览表来也!...