Scala Data Types with Examples | Escape Value & Typecasting

Free Scala course with real-time projects Start Now!!

Scala Data Types

Hello, readers! Welcome back to learning Scala with DataFlair. Today, we will discuss Scala Data Types with basic literals, Escape value, typecasting and its examples.

So, let’s start the Scala Data Types Tutorial.

Scala Data Types with Examples | Escape Value & Typecasting

Scala Data Types with Examples | Escape Value & Typecasting

Introduction to Data Types in Scala

Like every other language, Scala has a type for every value. We saw this when we discussed variables and values. Even functions have a value. So, let’s understand the Scala data types hierarchy for unified types.

Introduction to Scala Data Types

Introduction to Scala Data Types

Here, the supertype for all types is Any. It has universal methods like equals, hashCode, and toString.

Any parents two subclasses: AnyVal and AnyRef.

AnyVal represents value types. The nine predefined and non-nullable value types are: Double, Float, Long, Int, Short, Byte, Char, Unit, and Boolean. We’ll discuss these values in a short while. AnyRef represents reference types. A user-defined type is a subtype of this. And in the context of a JRE, AnyRef corresponds to java.lang.Object.

Lets now directly jump to the Scala Data Types.

Scala Data Types

Let’s discuss the basic in-built Scala data types in detail.

a. Scala Byte

Size: 8-bit
Signed value
Range: -128 to 127

b. Scala Short

Size: 16-bit
Signed value
Range: -32768 to 32767

c. Scala Int

Size: 32-bit
Signed value
Range: – 2147483648 to 2147483647

d. Scala Long

Size: 64-bit
Signed value
Range: -9223372036854775808 to 9223372036854775807

e. Scala Float

Size: 32-bit
It follows the IEEE 754 standard, and is a single-precision float.

f. Scala Double

Size: 32-bit
It follows the IEEE 754 standard, and is a double-precision float.

g. Scala Char

Size: 16-bit
It is an unsigned Unicode character
Range: U+0000 to U+FFFF

h. Scala String

A string is a sequence of Chars.

i. Scala Boolean

A Boolean value is either true or false.

j. Scala Unit

There is only one instance of unit, and that is (). It carries no meaningful information. And since all functions must return something, sometimes, we have them return Unit.

k. Scala Null

This refers to an empty or null reference. It is a subtype of all reference types. This makes it a subtype of any subtype of AnyRef. Null helps with interoperability with other JVM languages, and we almost never use it.

l. Scala Nothing

Nothing is a subtype to every other type. And trust us, it holds no value at all.
Since it is a subtype of all types, we also call it the bottom type. Actually, no value is of type Nothing. So where would we use it? We can use it to signal non-termination like a thrown exception, program exit, or an infinite loop.

m. Scala Any

This is the supertype for all other types. This means that any object is of the type Any.

n. Scala AnyVal

This represents value types.

o. Scala AnyRef

AnyRef represents reference types.

Since all these Scala Data types are objects and not primitives, it is possible to call methods on these objects.

Basic Literals

In this section, we will see integral, floating point, Boolean, symbol, character, and string literals in Scala. We will also see multi-line strings and null values.

a. Integral Literals

These are usually Ints. When we use an ‘l’ or ‘L’ suffix, these are Longs.
Some valid integral literals:
07
7
111
0xFFFFFFFF
0798L

b. Floating-Point Literals

We’ve seen floating-point numbers like 7.0 and 7.7 rather than 7. When there’s a suffix of ‘f’ or ‘F’, these are of type Float. Otherwise, they’re of type Double.
Some valid floating-point literals:
0.0
1e70f
3.24179f
1.0e100
.1

c. Boolean Literals

We have two Boolean literals- true and false.

d. Symbol Literals

Scala has a Symbol case class:

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

So, a symbol ‘x’ is equivalent to scala.Symbol(“x”).

e. Character Literals

This is a single character delimited by quotes. A character is a printable Unicode character, and can be described by an escape sequence. We’ll discuss escape sequences next.
Some valid character literals:
‘a’
‘\n’
‘\u0042’
‘\t’

f. String Literals

Such a literal is a sequence of characters delimited by double quotes.
Some valid string literals:
“Hannah\nMontana”
“And then she said, \”Be here now is a dog’s purpose\””

g. Multi-line Strings

Like in Python, we can use three sets of double quotes to delimit a string to span it across multiple lines.
A valid multi-line string:
“””The first line
The second line\n
The fourth line”””

h. Null Values

A null value has the type scala.Null. This makes it compatible with each reference type. But what is really denotes is a reference value referring to a special ‘null’ object.

Any doubt yet in Scala Data Types.

Escape Values

An escape value is a backslash with a character that will escape that character to execute a certain functionality. We can use these in character and string literals. We have the following escape values in Scala:

Escape SequencesUnicodeDescription
\b\u0008Backspace BS
\t\u0009Horizontal Tab HT
\n\u000aNewline
\f\u000cFormfeed FF
\r\u000dCarriage Return CR
\”\u0022Double quote “
\”\u0027Single quote ‘
\\\u005cBackslash \

We can also represent characters with Unicodes between 0 and 255 with octal escapes (using backslashes). Upto three octal characters may follow. Take an example:

scala> println("Hey, how \b are you?\n\tI've been\r...\"waiting\"")

Hey, how are you?
…”waiting” been

Type Casting

In Scala, we can convert one type to another:

Scala Data Types: Type Casting

Scala Data Types: Type Casting

Let’s take an example.

scala> val a:Int=7
a: Int = 7
scala> val b:Float=a
b: Float = 7.0
Hmm, let’s take another example to make it clear.
scala> val c:Char='a'
c: Char = a
scala> val d:Int=c
d: Int = 97
scala> val e:Char='A'
e: Char = A
scala> val f:Float=e
f: Float = 65.0

This was all on Scala Data Types

Conclusion

This is all about Scala Data Types. Doesn’t it feel just a bit different than Java and C++, yet so similar? Let us know in the comments if you have any doubts.

Reference

Your 15 seconds will encourage us to work even harder
Please share your happy experience on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *