Skip to Content
Scala Cookbook, 2nd Edition
book

Scala Cookbook, 2nd Edition

by Alvin Alexander
August 2021
Intermediate to advanced
799 pages
17h 41m
English
O'Reilly Media, Inc.
Content preview from Scala Cookbook, 2nd Edition

Chapter 3. Numbers and Dates

This chapter covers recipes for working with Scala’s numeric types, and it also includes recipes for working with the Date and Time API that was introduced with Java 8.

In Scala, the types Byte, Short, Int, Long, and Char are known as integral types because they are represented by integers, or whole numbers. The integral types along with Double and Float comprise Scala’s numeric types. These numeric types extend the AnyVal trait, as do the Boolean and Unit types. As discussed on the unified types Scala page, these nine types are called the predefined value types, and they are non-nullable.

The relationship of the predefined value types to AnyVal and Any (as well as Nothing) is shown in Figure 3-1. As shown in that image:

  • All of the numeric types extend AnyVal.

  • All other types in the Scala class hierarchy extend AnyRef.

Figure 3-1. All the predefined numeric types extend AnyVal

As shown in Table 3-1, the numeric types have the same data ranges as their Java primitive equivalents.

Table 3-1. The data ranges of Scala’s built-in numeric types
Data type Description Range

Char

16-bit unsigned Unicode character

0 to 65,535

Byte

8-bit signed value

–128 to 127

Short

16-bit signed value

–32,768 to 32,767

Int

32-bit signed value

–2,147,483,648 to 2,147,483,647

Long

64-bit signed value

–263 to 263–1, inclusive (see below)

Float

32-bit IEEE ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Scala Cookbook

Scala Cookbook

Alvin Alexander
Programming Scala, 2nd Edition

Programming Scala, 2nd Edition

Dean Wampler, Alex Payne
Functional Programming in Scala, Second Edition

Functional Programming in Scala, Second Edition

Runar Bjarnason, Michael Pilquist, Paul Chiusano

Publisher Resources

ISBN: 9781492051534Errata Page