English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Scala Data Types

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

Data TypeDescription
Byte8bit signed two's complement integer. The value range is -128 to 127
Short16bit 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
Float32 bit, IEEE 754 a standard single-precision floating-point number
Double64 bit IEEE 754 a standard double-precision floating-point number
Char16unsigned Unicode character, the value range is U+0000 to U+FFFF
Stringa sequence of characters
Booleantrue or false
UnitRepresents 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 ().
Nullnull or empty reference
NothingThe Nothing type is at the bottom of the Scala class hierarchy; it is a subtype of any other type.
AnyAny is the superclass of all other classes
AnyRefThe 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 basic literals

Scala is very simple and intuitive. In the following, we will introduce Scala literals in detail.

Integer literals

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

Floating-point literals

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

Boolean literals have true and false.

symbol literals

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
}

Character literals

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.

String literals

Double quotes are used in Scala string literals " defined as follows:

"Hello,
World!"
"Basic Tutorial website: www.oldtoolbag.com"

Representation method of multi-line strings

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 value

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.

Scala Escape Characters

The following table lists common escape characters:

Escape CharactersUnicodeDescription
\b\u0008Backspace (BS), moves the current position to the previous column
\t\u0009Horizontal Tab (HT) (jumps to the next tab position)
\n\u000aLine Feed (LF), moves the current position to the beginning of the next line
\f\u000cForm Feed (FF), moves the current position to the beginning of the next page
\r\u000dCarriage Return (CR), moves the current position to the beginning of the line
\"\u0022represents a double quote character (")
\'\u0027represents a single quote character (')
\\\u005crepresents 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