Scala data types


Scala has the same data types as Java. The following table lists the data types supported by Scala:

Data typeDescription
Byte8-bit signed two’s complement integer. The value range is -128 to 127
Short16-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
Float32-bit IEEE754 single-precision floating point number
Double64-bit IEEE754 single-precision floating point number
Char16-bit unsigned Unicode character, the range value is U+0000 to U+FFFF
StringCharacter sequence
Booleantrue 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 ().
Nullnull or null reference
NothingNothing type at the Scala class level The lowest end; it is a subtype of any other type.
AnyAny is the super class of all other classes
AnyRefThe 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:

Escape characters\b\t\n\f\r\"\'\\

Unicode characters between 0 and 255 can be represented by an octal escape sequence, i.e., a backslash \ followed by Up to three octals.

In a character or string, the backslash and the following character sequence cannot form a legal escape sequence, which will result in Compile Error.

The following example demonstrates the use of some escape characters:

object Test {
   def main(args: Array[String]) {
      println("Hello\tWorld\n\n" );
   }
}
Running example »

The output result of executing the above code is as follows:

$ scalac Test.scala
$ scala Test
Hello	World


$
UnicodeDescription
\u0008Backspace (BS), move the current position to the previous column
\u0009Horizontal tab (HT) (jump to the next TAB position)
\u000cLine feed (LF), moves the current position to the beginning of the next line
\u000cPage change (FF), move the current position to the beginning of the next page
\u000dPress 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 '\'