Fundamentals 7 min read

Scala Data Types and Literals Overview

This article introduces Scala’s built‑in data types, explains their characteristics, and demonstrates how to write various literals—including integers, floating‑point numbers, booleans, symbols, characters, strings, multi‑line strings, and null values—while also covering escape sequences and example code.

Big Data Technology & Architecture
Big Data Technology & Architecture
Big Data Technology & Architecture
Scala Data Types and Literals Overview

Scala shares the same data types as Java, but all of them are objects; there are no primitive types. The table below lists the Scala data types and their descriptions.

Data Type

Description

Byte

8‑bit signed two's‑complement integer, range -128 to 127.

Short

16‑bit signed two's‑complement integer, range -32768 to 32767.

Int

32‑bit signed two's‑complement integer, range -2147483648 to 2147483647.

Long

64‑bit signed two's‑complement integer, range -9223372036854775808 to 9223372036854775807.

Float

32‑bit IEEE‑754 single‑precision floating‑point number.

Double

64‑bit IEEE‑754 double‑precision floating‑point number.

Char

16‑bit unsigned Unicode character, range U+0000 to U+FFFF.

String

Sequence of characters.

Boolean

Either true or false.

Unit

Represents no value (similar to void); the only instance is ().

Null

Null reference.

Nothing

Bottom type; subtype of every other type.

Any

Supertype of all other types.

AnyRef

Base class of all reference types.

All listed types are objects, meaning Scala does not have primitive types like Java; methods can be invoked on numeric and other basic types directly.

Scala Basic Literals

Scala literals are straightforward and intuitive. The following sections detail each kind of literal.

Integer Literals

Integer literals are of type Int. To denote a Long, append L or l to the number.

0
035
21
0xFFFFFFFF
0777L

Floating‑Point Literals

If a floating‑point literal ends with f or F, it is a Float; otherwise it is a Double.

0.0
1e30f
3.14159f
1.0e100
.1

Boolean Literals

Boolean literals are true and false.

Symbol Literals

Symbol literals are written as '<identifier> and map to instances of scala.Symbol.

Example:

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

Character Literals

Characters are defined with single quotes.

'a'
'\u0041'
'
'
'\t'

The backslash introduces escape sequences such as \u0041, \n, \t, etc.

String Literals

Strings are enclosed in double quotes.

"Hello,
World!"
"scala tutorial website: www.scala-lang.org"

Multi‑Line String Literals

Three consecutive double quotes delimit a multi‑line string.

val foo = """菜鸟教程
www.runoob.com
www.w3cschool.cc
www.runnoob.com
以上三个地址都能访问"""

Null Value

The null value has type scala.Null. Null and Nothing are special types used to handle edge cases in Scala’s object‑oriented type system.

Escape Characters

Common escape characters are listed below.

Escape

Unicode

Description

\b

\u0008

Backspace (BS)

\t

\u0009

Horizontal tab (HT)

\n

\u000a

Line feed (LF)

\f

\u000c

Form feed (FF)

\r

\u000d

Carriage return (CR)

\"

\u0022

Double‑quote character

\'

\u0027

Single‑quote character

\\

\u005c

Backslash character

Unicode characters from 0 to 255 can also be represented with up to three octal digits after a backslash. Invalid escape sequences cause compilation errors.

Escape Character Example

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

" );
   }
}

Running the program produces:

$ scalac Test.scala
$ scala Test
Hello    World
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Data Typesprogramming fundamentalsScalaLiterals
Big Data Technology & Architecture
Written by

Big Data Technology & Architecture

Wang Zhiwu, a big data expert, dedicated to sharing big data technology.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.