Data types in Scala

As mentioned, Scala is a JVM language, so it shares lots in common with Java. One of these commonalities is the data types; Scala shares the same data types with Java. In short, Scala has all the same data types as Java, with the same memory footprint and precision. As mentioned in Chapter 1, Introduction to Scala, objects are almost everywhere in Scala. and all data types are objects and you can call methods in them as follows:

Sr.No

Data Type and Description

1

Byte: 8 bit signed value. Ranges from -128 to 127

2

Short: 16 bit signed value. Ranges -32768 to 32767

3

Int: 32 bit signed value. Ranges -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

Table 1: Scala data types, description, and range

All the data types listed in the preceding table are objects. However, note that there are no primitive types, as in Java. This means that you can call methods on an Int, Long, and so on.

val myVal = 20
//use println method to print it to the console you will also notice that if will be inferred as Int
println(myVal + 10)
val myVal = 40
println(myVal * "test")

Now, you can start playing around with these variables. Let's get some ideas on how to initialize a variable and work on the type annotations.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.168.2