scala变量类型和性质
最高的父类型为Any,最低类型为Nothing
Any is the supertype of all types, also called the top type. It defines certain universal methods such as equals, hashCode, and toString. Any has two direct subclasses: AnyVal and AnyRef
注意:hashCode,equals,toString方法在Any类型中
AnyVal represents value types.
There are nine predefined value types and they are non-nullable: Double, Float, Long, Int, Short, Byte, Char, Unit, and Boolean. Unit is a value type which carries no meaningful information. There is exactly one instance of Unit which can be declared literally like so: (). All functions must return something so sometimes Unit is a useful return type
值类型是不能为null,Unit代表不携带任何有意义的信息
//test AnyVal is not null-able
//compile error
//error msg an expression of type Null is ineligible for implicit conversion
val xi :Int = null
AnyRef represents reference types. All non-value types are defined as reference types. Every user-defined type in Scala is a subtype of AnyRef. If Scala is used in the context of a Java runtime environment, AnyRef corresponds to java.lang.Object
//AnyRef在Java运行环境中相当于java.lang.Object
类型转换
val x: Long = 987654321
val y: Float = x // 9.8765434E8 (note that some precision is lost in this case) val face: Char = '☺'
val number: Int = face // 9786
Casting is unidirectional. This will not compile
类型转换未按照图中的方向,不编译.
意思就是scala类型转换一定是安全的.不能由长类型转成短类型(这句话或许有错,因为Long可以转换成Float)
Char不能到Long应该是scala的编译不能识别这种转换,以后在爬这个事的原因
val x: Long = 987654321
val y: Float = x // 9.8765434E8
val z: Long = y // Does not conform
Nothing is a subtype of all types, also called the bottom type. There is no value that has type Nothing. A common use is to signal non-termination such as a thrown exception, program exit, or an infinite loop (i.e., it is the type of an expression which does not evaluate to a value, or a method that does not return normally).
Null is a subtype of all reference types (i.e. any subtype of AnyRef). It has a single value identified by the keyword literal null. Null is provided mostly for interoperability with other JVM languages and should almost never be used in Scala code. We’ll cover alternatives to null later in the tour.
Null在scala中用null代替,为了保证scala和其他Java虚拟机语言的互操作性
scala变量类型和性质的更多相关文章
- Scala变量(三)
变量是一种使用方便的占位符,用于引用计算机内存地址,变量创建后会占用一定的内存空间. 基于变量的数据类型,操作系统会进行内存分配并且决定什么将被储存在保留内存中.因此,通过给变量分配不同的数据类型,你 ...
- 20151010 C# 第一篇 变量类型
20151010 变量类型: 1. 值类型:变量本身直接存储数据 整数类型:代表没有小数点的整数数值 类型 说明 范围 sbyte 8位有符号整数 -128——127 short 16位有符号整数 - ...
- Scala基础类型与操作
Scala基本类型及操作.程序控制结构 Scala基本类型及操作.程序控制结构 (一)Scala语言优势 自身语言特点: 纯面向对象编程的语言 函数式编程语言 函数式编程语言语言应该支持以下特性: 高 ...
- Scala变量| 流程控制
Scala 是 Scalable Language 的简写,是一门多范式(编程的方式)的编程语言 Scala是一门以java虚拟机(JVM)为目标运行环境并将面向对象和函数式编程的最佳特性结合在一起的 ...
- 2. Scala变量
2.1 变量是程序的基本组成单位 举一个简单的例子 object boke_demo01 { def main(args: Array[String]): Unit = { var a: Int = ...
- Spark记录-Scala变量/访问修饰符/运算符
变量是保存存储值的内存位置的名称.这意味着当创建变量时,可以在内存中保留一些空间. 根据变量的数据类型,编译器分配内存并决定可以存储在预留内存中的内容.因此,通过为变量分配不同的数据类型,可以在这些变 ...
- Scala系统学习(四):Scala变量
变量是保存存储值的内存位置的名称.这意味着当创建变量时,可以在内存中保留一些空间. 根据变量的数据类型,编译器分配内存并决定可以存储在预留内存中的内容.因此,通过为变量分配不同的数据类型,可以在这些变 ...
- C的变量类型、作用域与生命周期的总结
C的变量类型.作用域与生命周期的总结 最近在看"C Programing Language" (Kernighan, Ritchie)关于外部变量的讨论,之前在学C的时候对这些ex ...
- Scala 基础(四):Scala变量 (一) 基础
1.概念 变量相当于内存中一个数据存储空间的表示,你可以把变量看做是一个房间的门 牌号,通过门牌号我们可以找到房间,而通过变量名可以访问到变量(值). 2 变量使用的基本步骤 1) 声明/定义变量 ( ...
随机推荐
- Vivado HLS初识---阅读《vivado design suite tutorial-high-level synthesis》(2)
Vivado HLS初识---阅读<vivado design suite tutorial-high-level synthesis>(2) 1.实验目的 2.启动命令行 将命令行切换 ...
- C++Builder 网站。记住学习
http://www.ccrun.com/ C++Builder
- 解析H5本地储存Web Storage
一.本地存储由来的背景 由于HTML4时代Cookie的大小.格式.存储数据格式等限制,网站应用如果想在浏览器端存储用户的部分信息,那么只能借助于Cookie.但是Cookie的这些限制,也就导致了C ...
- intellij idea 快捷输出 main方法的规律
今天偶然发现了IntelliJ中 创建main函数的快捷键,依次还有for循环,System.out.println(); 在编写代码的时候直接输入psv就会看到一个psvm的提示,此时点击tab键一 ...
- 自动化测试Java一:Selenium入门
From: https://blog.csdn.net/u013258415/article/details/77750214 Selenium入门 欢迎阅读Selenium入门讲义,本讲义将会重点介 ...
- Python:删除、增加字典的元素
一)增加一个或多个元素 d = {'a': 1} d.update(b=2) #也可以 d.update({‘b’: 2}) print(d) -->{'a': 1, 'b': 2} ...
- Ajax的总结
1.运行Ajax的环境,在服务器上才可以实现他的功能,客户端等别的地方,虽然也可以运行,但是功能一定是不全的,有可能很多东西都不会发生反应: 2.传参 (只写关键步骤) (必须在服务器上运行) ge ...
- go语言学习--channel的关闭
在使用Go channel的时候,一个适用的原则是不要从接收端关闭channel,也不要在多个并发发送端中关闭channel.换句话说,如果sender(发送者)只是唯一的sender或者是chann ...
- [UE4]位与字节
位 1.bit,比特 2.一个位可以表示两个值,0或者1(一个位只能表示0或者1,并不是能同时表示0和1). 3.一个位为什么只能是2个值,而不能是3个值呢?这是由于技术因素造成的,在硬件中,如果用一 ...
- [UE4]反射
1.根据名字获得类(C++支持,蓝图本身不支持但可以通过工厂模式模拟) 国外大神提供的封装好的C++实现: https://github.com/getsetgames/BlueprintReflec ...