English 中文(简体)
Scala - Data Types
  • 时间:2024-11-05

Scala - Data Types


Previous Page Next Page  

Scala has all the same data types as Java, with the same memory footprint and precision. Following is the table giving details about all the data types available in Scala −

Sr.No Data Type & Description
1

Byte

8 bit signed value. Range from -128 to 127

2

Short

16 bit signed value. Range -32768 to 32767

3

Int

32 bit signed value. Range -2147483648 to 2147483647

4

Long

64 bit signed value. -9223372036854775808 to 9223372036854775807

5

Float

32 bit IEEE 754 single-precision float

6

Double

64 bit IEEE 754 double-precision float

7

Char

16 bit unsigned Unicode character. Range from U+0000 to U+FFFF

8

String

A sequence of Chars

9

Boolean

Either the pteral true or the pteral false

10

Unit

Corresponds to no value

11

Null

null or empty reference

12

Nothing

The subtype of every other type; includes no values

13

Any

The supertype of any type; any object is of type Any

14

AnyRef

The supertype of any reference type

All the data types psted above are objects. There are no primitive types pke in Java. This means that you can call methods on an Int, Long, etc.

Scala Basic Literals

The rules Scala uses for pterals are simple and intuitive. This section explains all basic Scala Literals.

Integral Literals

Integer pterals are usually of type Int, or of type Long when followed by a L or l suffix. Here are some integer pterals −

0
035
21 
0xFFFFFFFF 
0777L

Floating Point Literal

Floating point pterals are of type Float when followed by a floating point type suffix F or f, and are of type Double otherwise. Here are some floating point pterals −

0.0 
1e30f 
3.14159f 
1.0e100
.1

Boolean Literals

The Boolean pterals true and false are members of type Boolean.

Symbol Literals

A symbol pteral x is a shorthand for the expression scala.Symbol("x"). Symbol is a case class, which is defined as follows.

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

Character Literals

A character pteral is a single character enclosed in quotes. The character is either a printable Unicode character or is described by an escape sequence. Here are some character pterals −

 a  
 u0041 
 
 
 	 

String Literals

A string pteral is a sequence of characters in double quotes. The characters are either printable Unicode character or are described by escape sequences. Here are some string pterals −

"Hello,
World!"
"This string contains a " character."

Multi-Line Strings

A multi-pne string pteral is a sequence of characters enclosed in triple quotes """ ... """. The sequence of characters is arbitrary, except that it may contain three or more consecutive quote characters only at the very end.

Characters must not necessarily be printable; newpnes or other control characters are also permitted. Here is a multi-pne string pteral −

"""the present string
spans three
pnes."""

Null Values

The null value is of type scala.Null and is thus compatible with every reference type. It denotes a reference value which refers to a special "null" object.

Escape Sequences

The following escape sequences are recognized in character and string pterals.

Escape Sequences Unicode Description
 u0008 backspace BS
u0009 horizontal tab HT
u000c formfeed FF
f u000c formfeed FF
u000d carriage return CR
" u0022 double quote "
u0027 single quote .
\ u005c backslash

A character with Unicode between 0 and 255 may also be represented by an octal escape, i.e., a backslash followed by a sequence of up to three octal characters. Following is the example to show few escape sequence characters −

Example

object Test {
   def main(args: Array[String]) {
      println("Hello	World

" );
   }
} 

When the above code is compiled and executed, it produces the following result −

Output

Hello   World
Advertisements