Scala data type

Scala has the same data type as Java, and the following table lists the data types supported by Scala:

data type Description
Byte 8-bit signed complement integer. The range of values ​​is -128 to 127
Short 16-bit signed complement integer. The range of values ​​is -32768 to 32767
Int 32-bit signed complement integer. The value range is -2147483648 to 2147483647
Long 64-bit signed complement integer. The value range is -9223372036854775808 to 9223372036854775807
Float 32-bit, IEEE 754 standard single-precision floating-point number
Double 64-bit IEEE 754 standard double-precision floating-point number
Char 16-bit unsigned Unicode character, interval value U+0000 to U+FFFF
String character sequence
Boolean true or false
Unit means no value, and is equivalent to void in other languages. The result type used as a method that does not return any results. Unit has only one instance value, written as ().
Null null or null reference
NothingNothing type is at the bottom of Scala's class hierarchy; it is any other type of subtype.
Any Any is a superclass of all other classes
AnyRef The AnyRef class is the base class for all reference classes in Scala

The data types listed in the above table are all objects, which means that scala does not have a native type in java. In scala, it is possible to call methods on basic types such as numbers.

Scala basic literals

Scala is very simple and intuitive. Next we will detail the Scala literals.

Integer literals

Integer literals are used for Int types. If you represent Long, you can add L or lowercase l as a suffix after the number. :


Floating type literals

If there is an f or F suffix after the floating point number, it means that this is a Float type, otherwise it is a Double type. An example is as follows:


Boolean literals

Boolean literals have true and false.

symbol literals

Symbol literals are written as: '<Identifier> ,Here <Identifier> Can be any letter or number identification (note: cannot begin with a number). This literal is mapped to an instance of the predefined class scala.Symbol.

such as: The symbol literal 'x is short for the expression scala.Symbol("x"), and the symbol literal is defined as follows:

package scala
final case class Symbol private (name: String) {
   override def toString: String = "'" + name

Character literals

The Scala character variable is defined using the single quotes ' as follows:


where \ represents an escape character followed by a fixed escape character such as a u0041 number or \r\n.

String literals

Use the double quotes " in Scala string literals to define as follows:

"Rookie Tutorial Website: www.google.com"

Representation of multi-line strings

Multi-line strings are represented by three double quotes in the format: """ ... """.

Examples are as follows:

val foo = """welookups tutorial
All of the above three addresses can access """

Null value

The null value is of type scala.Null.

Scala.Null and scala.Nothing are special types that deal with certain "boundary cases" of the Scala object-oriented type system in a uniform way.

The Null class is the type of the null reference object, which is a subclass of each reference class (the class that inherits from AnyRef). Null is not compatible with value types.

Scala escape character

The following table lists common escape characters:

escape character UnicodeDescription
\b \u0008 Backspace (BS) to move the current position to the previous column
\t \u0009 Horizontal Tabulation (HT) (skip to the next TAB location)
\n \u000a Wrap (LF) to move the current position to the beginning of the next line
\f \u000c Form feed (FF), move the current position to the beginning of the next page
\r \u000d Enter (CR) to move the current position to the beginning of the line
\" \u0022 represents a double quotation mark (") character
\' \u0027 represents a single quote (') character
\\ \u005c represents a backslash character '\'

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

In a character or string, a backslash and a subsequent sequence of characters cannot form a legal escape sequence that 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" );

Execute the above code output as follows:

$ scalac Test.scala
$ scala Test
Hello    World