Scala data types
Scala has the same data types as Java. The following table lists the data types supported by Scala:
Data type | Description |
---|---|
Byte | 8-bit signed two’s complement integer. The value range is -128 to 127 |
Short | 16-bit signed two's complement integer. The value range is -32768 to 32767 |
Int | 32-bit signed two's complement integer. The value range is -2147483648 to 2147483647 |
Long | 64-bit signed two's complement integer. The value range is -9223372036854775808 to 9223372036854775807 |
Float | 32-bit IEEE754 single-precision floating point number |
Double | 64-bit IEEE754 single-precision floating point number |
Char | 16-bit unsigned Unicode character, the range value is U+0000 to U+FFFF |
String | Character sequence |
Boolean | true or false |
Unit | means no value, which is equivalent to void in other languages. Used as the result type for methods that return no results. Unit has only one instance value, written as (). |
Null | null or null reference |
Nothing | Nothing type at the Scala class level The lowest end; it is a subtype of any other type. |
Any | Any is the super class of all other classes |
AnyRef | The AnyRef class is Scala The base class of all reference classes in |
The data types listed in the above table are all objects, which means that scala does not have native types in java. In Scala, you can call methods on basic types such as numbers.
Scala Basic Literals
Scala is very simple and intuitive. Next we will introduce Scala literals in detail.
Integer literal
Integer literal is used for Int type. If it represents Long, you can add L or lowercase l as a suffix after the number. :
0 035 21 0xFFFFFFFF 0777L
Floating point literal
If there is f or F suffix after the floating point number, it means that it is a Float type, otherwise it is a Double type. Examples are as follows:
0.0 1e30f 3.14159f 1.0e100 .1
Boolean literal
Boolean literals include true and false.
Symbol literal
The symbol literal is written as: '<identifier>, where <identifier> can be Any alphanumeric identifier (note: cannot begin with a number). This literal is mapped to an instance of the predefined class scala.Symbol.
like: Symbol literal 'x is the abbreviation of the expression scala.Symbol("x"). The symbol literal is defined as follows:package scala final case class Symbol private (name: String) { override def toString: String = "'" + name }
characters The literal
in scala is represented by the character type in half-width single quotes ('), as follows:
'a' '\u0041' '\n' '\t'
where \ represents the transfer character, which can be followed by u0041 Numbers or fixed escape characters such as \r\n.
String literal
The string representation method is to include a series of characters in double quotes ("), such as:
"Hello,\nWorld!" "php中文网官网:www.php.cn"
The representation method of multi-line string
Use three double quotes to represent the delimiter in multi-line strings, the format is: """ ... """. Examples are as follows:
val foo = """php中文网 www.php.cn www.w3cschool.cc www.runnoob.com 以上三个地址都能访问"""
Null value
The null value is the scala.Null type.
Scala.Null and scala.Nothing are used to handle certain "edge cases" of the Scala object-oriented type system in a unified way. "'s special type.
The Null class is the type of null reference object, which is a subclass of every reference class (the class that inherits from AnyRef). Null is not compatible with value types.
Scala Escape characters
The following table lists common escape characters:
Unicode | Description | |
---|---|---|
\u0008 | Backspace (BS), move the current position to the previous column | |
\u0009 | Horizontal tab (HT) (jump to the next TAB position) | |
\u000c | Line feed (LF), moves the current position to the beginning of the next line | |
\u000c | Page change (FF), move the current position to the beginning of the next page | |
\u000d | Press Enter (CR), Move the current position to the beginning of this line | |
\u0022 | represents a double quote (") character | |
\u0027 | represents a single quote (') character | |
\u005c | represents a backslash character '\' |