scala数据类型_Scala数据类型示例教程

scala数据类型

A data type tells the compiler about the type of the value to be stored in a location. There are no primitive data types unlike in java since everything in Scala is an object.

数据类型告诉编译器要存储在位置中的值的类型。 与Java中不同,没有原始数据类型,因为Scala中的所有内容都是一个对象。

The following data types are treated as objects in Scala and can call methods on int, String, long etc.

以下数据类型在Scala中被视为对象,并且可以调用int,String,long等方法。

Byte: Byte is an 8 bit signed value.The value ranges from -128 to 127

字节:字节是一个8位带符号的值,范围从-128到127

Short: is an 16 bit signed value ranging from -32768 to 32767

短:是一个16位有符号值,范围为-32768到32767

Int: is an 32 bit signed value ranging from -2147483648 to 2147483647

Int:是一个32位有符号值,范围为-2147483648至2147483647

Long: is a 64 bit signed value ranging from -9223372036854775808 to 9223372036854775807

Long:是一个64位带符号的值,范围从-9223372036854775808到9223372036854775807

Float: is a 32 bit single precision floating point.

浮点数: 32位单精度浮点数。

Double: is a 64 bit double precision floating point

Double:是64位双精度浮点数

Char: is a 16 bit unsigned unicode character ranging from U+0000 to U+FFFF.

字符: 16位无符号Unicode字符,范围从U + 0000到U + FFFF。

String: A sequence of characters enclosed with double quotes.

字符串:用双引号引起来的字符序列。

Boolean: literal with true and false values

布尔值:带真值和假值的文字

AnyRef: Supertype of any reference

AnyRef:任何参考的超类型

Nothing: Subtype of every other type includes no values

无:其他所有类型的子类型均不包含任何值

Null: Empty reference

空:空引用

Unit: Corresponds to no value

单位:无值

Any: Super type of any type or object of any type

任何:任何类型的超级类型或任何类型的对象

Scala基本文字 (Scala Basic Literals)

The basic literals include the following;

基本文字包括:

Integer literals: Integer literals are of type int,long followed by L or l suffix.Some of the examples of integer literals are 42,42FFF etc..

整数文字 :整数文字的类型为int,long后面带有L或l后缀。一些整数文字的示例为42,42FFF等。

Floating point Literals: This data type includes float followed by suffix F or f.Examples include 3.12,4.67 etc..

浮点字面量 :此数据类型包括浮点数,后跟后缀F或f,示例包括3.12、4.67等。

String Literals: A string is a sequence of characters enclosed within the double quotes.

字符串文字 :字符串是用双引号引起来的一系列字符。

Examples of strings are “Hello World”, “Set of Strings” etc.

字符串的示例是“ Hello World”,“字符串集”等。

Character literals: A character is a single character enclosed in quotes.

字符文字 :字符是用引号引起来的单个字符。

Examples include ‘\n’, ‘\t’ etc.

示例包括“ \ n”,“ \ t”等。

Multiline Strings: A multiline string are the characters enclosed with triple quotes “”” … “””. Newlines and other controllable characters are permitted and the sequence may be arbitrary. Example of a multiline string is

多行字符串 :多行字符串是用三引号“””…“””括起来的字符。 允许使用换行符和其他可控制字符,并且顺序可以是任意的。 多行字符串的示例是

“””String is a
sequence of
characters.”””

“””字符串是一个
的顺序
字符。”””

NULL value: The null value denotes a reference value which refers to a null object and null value is of type scala.Null.

NULL值 :NULL值表示引用值,该引用值引用NULL对象,并且NULL值是scala.Null类型。

Escape Sequence: The following escape sequences are supported in Scala

转义序列 :Scala支持以下转义序列

\b: backspace

\ b:退格键

\t: Horizontal tab

\ t:“水平”标签

\f: form feed

\ f:换页

\n: newline character

\ n:换行符

\r: carriage return

\ r:回车

\”: double quote

\”:双引号

\’: single quote

\':单引号

\\: backslash

\\:反斜杠

Let us see an example of the escape sequence;

让我们看一个转义序列的例子。

object String {
def main(args: Array[String]) {
println("String\tLiteral\n\ndatatype" );
}
}

Run the above code in the shell as shown below

如下所示在外壳中运行以上代码

scala>String.main(null)
String   Literal

datatype

scala>

This output shows that a tab space has been inserted between String and Literal and a new line is inserted printing the datatype. Below image shows it’s execution in Scala shell.

此输出表明在String和Literal之间插入了一个制表符空间,并插入了换行以打印数据类型。 下图显示了它在Scala shell中的执行。

That’s all for Scala data types, we will look into more Scala core features in the coming posts.

这就是Scala数据类型的全部内容,我们将在以后的文章中探讨更多Scala核心功能。

翻译自: https://www.journaldev.com/7578/scala-data-types-example-tutorial

scala数据类型

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值