Latest web development tutorials

Scala data types

Scala and Java have the same data type, the following table lists the data types supported Scala:

type of data description
Byte 8-bit signed integer complement. Value range is -128 to 127
Short 16-bit signed integer complement. Value range -32768 to 32767
Int 32-bit signed integer complement. Value range -2147483648 to 2147483647
Long 64-bit signed integer complement. Numerical interval -9223372036854775808 to 9223372036854775807
Float 32-bit single precision floating point IEEE754
Double 64-bit single precision floating point IEEE754
Char 16 unsigned Unicode character U + 0000 to a value range U + FFFF
String Character sequence
Boolean true or false
Unit It represents no value, and other languages ​​void equivalents. Used as a method does not return any results result type. Unit is only one instance of the value, written ().
Null null or empty references
Nothing Nothing types Scala's class hierarchy most low-end; it is any other type of subtype.
Any Any superclass of all other classes
AnyRef AnyRef class in Scala is all referenced classes (reference class) of the base class

Object data types are listed in the table, which means that there is no java scala of native types. In the scala can call to the other basic type of digital.

Scala basis literal

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

Integer literals

Int type for integer literals, if expressed Long, can be added as a suffix L or a lowercase l after the number. :


Float literals

If there is behind the float f or F suffix indicates that this is a Float type, otherwise a Double type. Examples are as follows:


Boolean Literals

Boolean literals have true and false.

Literal symbols

Symbol literal is written:'<identifier>, where <identifier>can be any letter or number identification (Note: You can not start with a number). This literal is mapped to an instance of the predefined class scala.Symbol.

Such as: literal symbol'x is the expression scala.Symbol ( "x")shorthand symbol literals are defined as follows:

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

Character literal

Type the characters represented in the scala half-width single quote ( ') characters, as follows:


Where\ represents the transfer of characters, numbers, or may be followed u0041\ r \ n and other fixed escape character.

String literal

String representation is in double quotes ( ") contains a series of characters, such as:


Representation multi-line strings

Multi-line strings using three pairs of quotation marks to indicate the delimiter, format:"" "..." "."

Examples are as follows:

val foo = """本教程

Null value

Null is scala.Null type.

Scala.Null and scala.Nothing is a unified approach to object-oriented type system of Scala certain "boundary conditions" of a special type.

Null type is the type of null object reference, which is a reference to each class (inherited from AnyRef class) subclass. Null values ​​are not compatible types.

Scala escape character

The following table lists common escape characters:

Escape character Unicode description
\ B \ U0008 Backspace (BS), the current position to the previous one
\ T \ U0009 Horizontal tab (HT) (skip to the next TAB position)
\ N \ U000c Line feed (LF), the current position to the beginning of the next line
\ F \ U000c Form feed (FF), the current position to the beginning of the next page
\ R \ U000d A carriage return (CR), the current position to the beginning of the line
\ " \ U0022 Represents a double quotation mark ( ") character
\ ' \ U0027 On behalf of a single quote ( ') character
\\ \ U005c Represent a backslash character '\'

Unicode character 0-255 can use an octal escape sequence to indicate that the backslash "\" followed by up to three octal.

The character or string of characters sequence backslash and the following can not constitute a valid escape sequence will result in a 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 instance »

Execute the above code output results are as follows:

$ scalac Test.scala
$ scala Test
Hello	World