HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to ScalaVariables (val / var)Data TypesStringsListsConditions (if / match)Loops (for / while)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables & Types
🔢 Numbers & Math
✍️ String
📚 Collection
🔀 Condition & Loop
🧩 Function
Scala›Data Types

Data Types

Int, Double, Boolean, String, and type conversions.

Simple enough for a 5-year-old to understand

Basic types

Example code
1
val number = 7
Int — whole number.
VariableInt — whole number.
2
val price = 3.14
Double — decimal number.
VariableDouble — decimal number.
3
val isOpen = true
Boolean.
VariableBoolean.
4
val text = "Scala"
String.
VariableString.
Variable
💡 Hint: Use a dot for decimals, not a comma.

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.What is the type of the value 3.14?

About this topic

In Scala, everything is an object; even numbers and booleans. The distinction between primitive types and wrapper classes in Java is not visible in the source code; the compiler chooses the efficient one itself.

At the top of the type hierarchy is Any; beneath it, value types and reference types diverge. This arrangement makes it possible to write generic code that processes different types together and forms the basis of collection design.

Scala takes a clear stance on null values: null exists for Java compatibility, but is expected not to be used. You express that a value may be absent using the Option type; thus, the compiler does not allow you to skip the check.

What you will learn

  • Basic types and the hierarchy
  • The behavior of integer division
  • The Option type
  • Type conversions
  • The distinction between value and reference types

Who is this for?

  • Learners of the Scala type system
  • Those who want to get rid of null pointer errors
  • Those surprised by the division result

When to use?

Every field that can be missing should be modeled with Option; in data processing errors, this prevents missing records from silently producing errors.

Double produces rounding errors in money calculations; using BigDecimal is standard in financial code.

Common mistakes

Wrong

val a = 7
val b = 2
println(a / b)

When two integers are divided, the result is still an integer and the decimal part is discarded; 3 is printed on the screen. This is the rule of the language, but it silently produces incorrect results in calculations. For a decimal result, at least one side must be decimal.

Correct

val a = 7
val b = 2
println(a.toDouble / b)

Wrong

val ad: String = null
println(ad.length)

Calling a method on a null reference stops the program with a NullPointerException. Scala allows null but does not recommend using it; when you express that a value might be absent with Option, the compiler forces you to check it.

Correct

val ad: Option[String] = None
println(ad.map(_.length).getOrElse(0))

Wrong

val metin = "42x"
val sayi = metin.toInt
println(sayi)

When the conversion fails, a NumberFormatException is thrown and the program stops. This is inevitable with user input. Using the version that does not throw errors and returns an empty result on failure is much safer.

Correct

val metin = "42x"
val sayi = metin.toIntOption.getOrElse(0)
println(sayi)

Frequently asked questions

How exactly does Option work?

It has two states: value exists or does not exist. You cannot access the value directly; you need to transform it, provide a fallback value, or unwrap it with pattern matching. This requirement makes it impossible to skip the null check.

What are Any, AnyVal, and AnyRef?

Any is the common ancestor of all types. AnyVal covers value types, while AnyRef covers object references. Knowing this hierarchy when writing generic code ensures you correctly determine which types are accepted.

How is type conversion done?

There are conversion methods for numeric types, and data loss can happen silently; for example, when converting from floating-point to integer, the fractional part is discarded. If you want rounding, you must write it explicitly.

Summary

In Scala, everything is an object.

The division of two integers discards the decimal part.

null can be used but is not recommended.

Values that may be missing are modeled with Option.

Non-throwing versions of conversions should be preferred.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code