JAVA DEVELOPER SITE
 

Scala - Data Types


In this chapter various types of Scala data type Scala have same data types as Java

Various 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 literal true or the literal 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 listed above are objects. There are no primitive types like in Java. This means that you can call methods on an Int, Long, etc.

Scala Basic Literals

The rules Scala uses for literals are simple and intuitive.

Integer Literals

nteger literals are usually of type Int,or of type Long when followed by a L or l suffix.
0
039
55
0xFFFFFFFF 
0206L

Floating Point Literal

Floating point literals are of type Float when followed by a floating point type suffix F or f, and are of type Double otherwise.
0.0 
1e30f 
2.1559f 
1.0e100
.1

Boolean Literals

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

Symbol Literals

A symbol literal 'a is a shorthand for the expression scala.Symbol("a").
package scala
final case class Symbol private (name: String) {
   override def toString: String = "'" + name
}

Character Literals

A character literal is a single character enclosed in quotes.
'a' 
'\u0041'
'\n'
'\t'

String Literals

A string literal is a sequence of characters in double quotes.
"Hello,\nWorld! Welookups"
"This string contains a \" character."

Escape Sequences

The following escape sequences are recognized in character and string literals.
Unicode Description
\b \u0008 backspace BS
\t \u0009 horizontal tab HT
\n \u000c formfeed FF
\f \u000c formfeed FF
\r \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
object Test {
   def main(args: Array[String]) {
      println("Hello\tWelookups\n\n" );
   }
} 
When the above code is compiled and executed

Output

Hello Welookups