English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Scala has the same data types as Java, the following table lists the data types supported by Scala:
Data Type | Description |
---|---|
Byte | 8bit signed two's complement integer. The value range is -128 to 127 |
Short | 16bit signed two's complement integer. The value range is -32768 to 32767 |
Int | 32bit signed two's complement integer. The value range is -2147483648 to 2147483647 |
Long | 64bit signed two's complement integer. The value range is -9223372036854775808 to 9223372036854775807 |
Float | 32 bit, IEEE 754 a standard single-precision floating-point number |
Double | 64 bit IEEE 754 a standard double-precision floating-point number |
Char | 16unsigned Unicode character, the value range is U+0000 to U+FFFF |
String | a sequence of characters |
Boolean | true or false |
Unit | Represents a valueless entity, equivalent to void in other languages. Used as the return type of methods that do not return any result. Unit has only one instance value, written as (). |
Null | null or empty reference |
Nothing | The Nothing type is at the bottom of the Scala class hierarchy; it is a subtype of any other type. |
Any | Any is the superclass of all other classes |
AnyRef | The AnyRef class is the base class for all reference classes (reference class) in Scala |
The data types listed in the table are objects, which means Scala does not have primitive types like Java. In Scala, you can call methods on basic types such as numbers.
Scala is very simple and intuitive. In the following, we will introduce Scala literals in detail.
Integer literals are used for Int type, and if representing Long, you can add L or lowercase l as a suffix to the number.:
0 035 21 0xFFFFFFFF 0777L
If a floating-point number has the suffix f or F, it indicates that it is a Float type; otherwise, it is a Double type. Example:
0.0 1e30f 3.14159f 1.0e100 .1
Boolean literals have true and false.
symbol literals are written as: '<identifier> , here <identifier> can be any identifier consisting of letters or numbers (note: cannot start with a number). This literal is mapped to an instance of the predefined class scala.Symbol.
such as: symbol literals 'x is an expression scala.Symbol("x") abbreviation, symbol literals are defined as follows:
package scala final case class Symbol private (name: String) { override def toString: String = "'" + name }
Single quotes are used in Scala character variables ' defined as follows:
'a' '\u0041' '\n' '\t'
Among them \ represents an escape character, which can be followed by u0041 number or \r\n etc. fixed escape characters.
Double quotes are used in Scala string literals " defined as follows:
"Hello, World!" "Basic Tutorial website: www.oldtoolbag.com"
Multi-line strings are represented by three double quotes as delimiters, in the format:""" ... """.
Here is an example:
val foo = """Basic Tutorial" www.oldtoolbag.com www.oldtoolbag.com www.runnoob.com All three addresses can access ""
Null is of type scala.Null.
Scala.Null and scala.Nothing are special types that handle some "boundary cases" of Scala's object-oriented type system in a unified way.
The Null class is the type of null reference objects, which is a subclass of each reference type (a class inheriting from AnyRef). Null is not compatible with value types.
The following table lists common escape characters:
Escape Characters | Unicode | Description |
---|---|---|
\b | \u0008 | Backspace (BS), moves the current position to the previous column |
\t | \u0009 | Horizontal Tab (HT) (jumps to the next tab position) |
\n | \u000a | Line Feed (LF), moves the current position to the beginning of the next line |
\f | \u000c | Form Feed (FF), moves the current position to the beginning of the next page |
\r | \u000d | Carriage Return (CR), moves the current position to the beginning of the line |
\" | \u0022 | represents a double quote character (") |
\' | \u0027 | represents a single quote character (') |
\\ | \u005c | represents a backslash character '\' |
from 255 Unicode characters between can be represented by an octal escape sequence, that is, a backslash "\" followed by up to three octal digits.
In characters or strings, an escape sequence consisting of a backslash and the following character sequence is not a valid escape sequence, which will cause Compilation Error.
The following example demonstrates the use of some escape characters:
object Test { def main(args: Array[String]) { println("Hello\tWorld\n\n"); } }
The output after running is as follows:
$ scalac Test.scala $ scala TestHello World